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。所以当我启动它时,数据只是容器元素,所以我不知道如何读取它并将每个项目转到我的 Arraylist 的下一个元素,该 Arraylist 进入存储每个项目和信息的 Class CateogoryInfo。每个都有标题和项目详细信息或多个项目详细信息。

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 回答