Parse XML using DOM in android

后端 未结 5 2021
梦如初夏
梦如初夏 2020-12-22 13:39

Hi i want to parse XML and display list based on selection of user

my xml is looking like this

\"enter

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 14:28

    This should do it:

            XMLParser parser = new XMLParser();
            Document doc = parser.getDomElement(xml); // getting DOM element
            NodeList n1 = doc.getElementsByTagName("company");
    
            // looping through all item nodes 
            for (int i = 0; i < n1.getLength(); i++) {
                Element e = (Element) n1.item(i);
                System.out.println("name node "  +parser.getValue(e, "name"));
                NodeList children = e.getChildNodes();
                for (int j = 0; j < children.getLength(); j++) {
                     Node child = children.item(j);
                     if (child.getNodeName().equalsIgnoreCase("province")) {
                          System.out.println("name node " + parser.getValue((Element)child, "name"));
                     }
                }
            }
    

提交回复
热议问题