0
我的XML是如下SAX解析器XML援助
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Item>
<CategoryId>1</CategoryId>
<Date>20121214</Date>
<Source>[email protected]</Source>
<ItemDetail>
<quality1>Morning</quality1>
<quality2>12010109Y</quality2>
<quality3>2.00</quality3>
<quality4>7.99</quality4>
</ItemDetail>
<ItemDetail>
<quality1>Afternoon</quality1>
<quality2>12010109B</quality2>
<quality3>900</quality3>
<quality4>83.50</quality4>
</ItemDetail>
<ItemDetail>
<quality1>Dinner</quality1>
<quality2>11271581Y</quality2>
<quality3>16.00</quality3>
<quality4>8.99</quality4>
</ItemDetail>
</Item>
<Item>
<CategoryId>2</CategoryId>
<Date>20121214</Date>
<Source>[email protected]</Source>
<ItemDetail>
<quality1>Morning</quality1>
<quality2>12010109Y</quality2>
<quality3>2.00</quality3>
<quality4>7.99</quality4>
</ItemDetail>
</Item>
<Item>
<CategoryId>3</CategoryId>
<Date>20121214</Date>
<Source>[email protected]</Source>
<ItemDetail>
<quality1>Afternoon</quality1>
<quality2>12010109B</quality2>
<quality3>900</quality3>
<quality4>83.50</quality4>
</ItemDetail>
</Item>
</Data>
這是我ParserClass
public class OrderXMLHandler extends DefaultHandler {
boolean currentElement = false;
String currentValue = "";
CategoryInfo itemdetail;
CategoryInfo categoryInfo;
ArrayList<CategoryInfo> List;
public ArrayList<CategoryInfo> getList() {
return List;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
/* if (qName.equals("Data")){
//List = new ArrayList<CategoryInfo>();
} */
if (localName.equals("maintag"))
{}
else if (qName.equals("Item")) {
List = new ArrayList<CategoryInfo>();
;
}
else if (qName.equals("ItemDetail")) {
categoryInfo = new CategoryInfo();
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if (qName.equalsIgnoreCase("CategoryId"))
{
categoryInfo.setCategoryId(currentValue.trim());
listId = currentValue.trim();
}
else if (qName.equalsIgnoreCase("Date"))
{//categoryInfo.setDate(currentValue.trim());
date = currentValue.trim();}
else if (qName.equalsIgnoreCase("Source"))
{//categoryInfo.setSource(currentValue.trim());
source = currentValue.trim();}
else if (qName.equalsIgnoreCase("quality1"))
categoryInfo.setQuality1(currentValue.trim());
else if (qName.equalsIgnoreCase("quality2"))
categoryInfo.setQuality2(currentValue.trim());
else if (qName.equalsIgnoreCase("quality3"))
categoryInfo.setQuality3(currentValue.trim());
else if (qName.equalsIgnoreCase("quality4"))
categoryInfo.setQuality4(currentValue.trim());
else if (qName.equalsIgnoreCase("ItemDetail"))
List.add(categoryInfo);
currentValue = "";
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
}
我不認爲我有它正確地分析了我的XML我做了。所以當我啓動它時,數據只是容器元素,所以我不知道如何讀取它,並將每個項目的下一個元素轉到我的Arraylist中,該元素進入Class CateogoryInfo存儲每個項目和信息。每個人都有標題和項目細節或多個項目細節。