0

私の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の各アイテムの次の要素に移動する方法がわかりません。それぞれにヘッダーとアイテムの詳細、または複数のアイテムの詳細があります。

4

1 に答える 1

0
 ArrayList<HashMap<String, String>> menuItems = new
 ArrayList<HashMap<String, String>>();

         XMLParser parser = new XMLParser();
         String xml = parser.getXmlFromUrl(URL); // getting XML
         Document doc = parser.getDomElement(xml); // getting DOM element

         NodeList nl = doc.getElementsByTagName("Item");
         // looping through all item nodes <item>
         for (int i = 0; i < nl.getLength(); i++) {
             // creating new HashMap
             HashMap<String, String> map = new HashMap<String, String>();
             Element e = (Element) nl.item(i);
             // adding each child node to HashMap key => value
             map.put(KEY_ID, parser.getValue(e, "CategoryId"));
             map.put(KEY_NAME, parser.getValue(e, "Source"));

                NodeList n2 = doc.getElementsByTagName("ItemDetail");
          for (int i = 0; i < n2.getLength(); i++) {
             map.put(KEY_COST, "Rs." + parser.getValue(e, "quality1"));
             map.put(KEY_DESC, parser.getValue(e, "quality2"));
           }
             // adding HashList to ArrayList
             menuItems.add(map);
         }

このように使用してください..あなたの解決策はここで行われると思います..

于 2012-12-15T06:45:14.580 に答える