android XML解析多个子节点

时间:2014-04-04 09:24:33

标签: android xml xml-parsing

我正在尝试解析xml,但它返回所有具有标记" nature"的chld节点。但我只想要那些猫被选中的节点。

我的xml看起来像: -

<NatureList>
  <NatureCategory cat="Crimes Against People">
<Nature>
<Name>Aggravated Assault</Name>
</Nature>
<Nature>
<Name>Annoyance Phone Calls</Name>
</Nature>
<NatureCategory>
<NatureCategory ...NatureCategory>

并且解析完成如下:

Document doc = XmlParser.getDomElement(xml);
    NodeList n = doc.getElementsByTagName("NatureCategory");

        try {


        for(int i=0; i<n.getLength();i++)
        {
            Element e = (Element) n.item(i);

            if(e.getAttribute("cat").equals(spinn.getSelectedItem().toString())) 
            {
                Log.i("sub1", spinn.getSelectedItem().toString()  );
                NodeList n1 = doc.getElementsByTagName("Nature");
                for(int j=0; j<n1.getLength();j++)
                {
                    Element e1 = (Element) n1.item(j);
                    subcategory.add(getValue(e1, "Name"));


                }
            }
        //  subcategory.add()

        }
        }

2 个答案:

答案 0 :(得分:1)

nodes1 = doc.getElementsByTagName("NatureList");                  

    for (int i = 0; i < nodes1.getLength(); i++) {

        ObjectClass cgro = new ObjectClass();
        Element e = (Element)nodes1.item(i);
        cgro.NatureCategory = XMLfunctions.getValue(e, "NatureCategory");//here u can compare with your spinner string if match then parse this NatureCategory or save oe any logic which u like 


        nodes1a = doc.getElementsByTagName("cat");

        for(int j = 0; j < nodes1a.getLength(); j++ ){

            ObjectClass1 cgro1 = new ObjectClass1();

             Element e2= (Element) nodes1a.item(j);
             cgro1.cat= XMLfunctions.getCharacterDataFromElement(e2);
             ArrayListClass.ItemList1.add(cgro1);
        }


        ArrayListClass.ItemList2.add(cgro);

        }

和此类使用

public class XMLfunctions {

    public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

    DocumentBuilder db = dbf.newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
    System.out.println("XML parse error: " + e.getMessage());
    return null;
    } catch (SAXException e) {
    System.out.println("Wrong XML file structure: " + e.getMessage());
    return null;
    } catch (IOException e) {
    System.out.println("I/O exeption: " + e.getMessage());
    return null;
    }

    return doc;

    }

    /** Returns element value
    * @param elem element (it is XML tag)
    * @return Element value otherwise empty String
    */

答案 1 :(得分:0)

我认为如果像这样形成你的XML会更好:

<NatureList>
  <NatureCategory>     
      <cat>Crimes Against People</cat>    
      <Nature>
        <Name>Aggravated Assault</Name>
    </Nature>

    <Nature>
         <Name>Annoyance Phone Calls</Name>

    </Nature>
    <NatureCategory>

然后获取cat标签,然后进行测试。