Parse XML using DOM in android

后端 未结 5 2028
梦如初夏
梦如初夏 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:27

    Use Node.getChildNodes() over the "company" nodes. Then, to get the child province nodes, compare by name. Example:

        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++) {
            Node companyNode = n1.item(i);
            NodeList childNodes = companyNode.getChildNodes();
            // Here we're getting child nodes inside the company node.
            // Only direct childs will be returned (name and province)  
    
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node childNode = childNodes.item(j);
                if("province".equalsIgnoreCase(childNode.getName())){
                    //Do something with province
                }
            }
        }
    

提交回复
热议问题