私のXMLは次のとおりです
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Item>
<CategoryId>1</CategoryId>
<Date>20121214</Date>
<Source>mysamplecode@gmail.com</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>code@gmail.com</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>code@gmail.com</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を正しく解析しているとは思いません。SOを開始すると、データは単なるコンテナ要素であるため、それを読み取って、各アイテムと情報を格納するクラスCateogoryInfoに入るArraylistの各アイテムの次の要素に移動する方法がわかりません。それぞれにヘッダーとアイテムの詳細、または複数のアイテムの詳細があります。