简体   繁体   中英

XML parsing. How can I get child's child?

I'm trying to parse this XML in Java:

<entities>
<entity name="product_section" id="1">
    <product_type>3</product_type>
    <section_type>1</section_type>
    <name>Empresa</name>
    <description>d</description>
    <position>1</position>
    <align>left</align>

    <files section_id="1">
        <ico id="ico_1" type="normal" src="sections/1/icons/ico.png"></ico>
        <ico id="ico_2" type="hover"  src="sections/1/icons/ico.png"></ico>
        <ico id="ico_3" type="active" src="sections/1/icons/ico.png"></ico>

        <img id="img_1" type="normal" src="sections/1/img/pestanya.png"></img>
        <img id="img_2" type="hover"  src="sections/1/img/pestanya-hover.png"></img>
        <img id="img_3" type="active" src="sections/1/img/pestanya-active.png"></img>

        <background id="background_1" type="background" position="1" src="sections/1/background/bg1.png"></background>
        <background id="background_2" type="background" position="2" src="sections/1/background/bg2.png"></background>
        <background id="background_3" type="background" position="3" src="sections/1/background/bg3.png"></background>
    </files>
</entity>

But I just achieved to loop through Entities, getting all Entity and each <product_type> , <section_type> , etc. But I want to loop through files too.

This is my implementation so far:

try {
        File contingut = new File("xmlfile.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(contingut);
        doc.getDocumentElement().normalize();
        System.out.println("root of xml file " + doc.getDocumentElement().getNodeName());
        //loop a cada entity
        NodeList nodes = doc.getElementsByTagName("entity");
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Element element = (Element) node;
            System.out.println("product_type: " + getValue("product_type", element));
            System.out.println("section_type: " + getValue("section_type", element));
            System.out.println("name: " + getValue("name", element));
            System.out.println("description: " + getValue("description", element));
            System.out.println("position: " + getValue("position", element));
            System.out.println("align: " + getValue("align", element));
        }
    } catch (Exception e){
        e.printStackTrace();
    }

getValue function is:

private static String getValue(String tag, Element element) {
    NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
    Node node = (Node) nodes.item(0);
    return node.getNodeValue();
}

I've done lot of google search, and all I find are "simple" examples, with a parent, and a child, but not child's child.

Any help would be appreciated.

At first one suggestion:

check element type after this Element element = (Element) node;

use this code or something like this :

if (element.getNodeType() == Element.ELEMENT_NODE) {  // do smth}

and answer to your question:

You can simply rewrite you code. after you create element you can get all it's child elements by using element.getChildNodes();

it gives you all child tags. After that you write simple for loop where you get each node element from node list like this :

NodeList nodes = element.getChildNodes();
for(int i =0; i < nodes.getLength(); i++){
     Element child = (Element) nodes.item(i);
     if(child.getNodeType() == Element.ELEMENT_NODE){
             String tagName = child.getTagName();
             if(!tagName.equals("files")){
                   System.out.println(tagName + " : " + child.getTextContent());
             }else{
                   NodeList filesChilds = child.getChildNodes();
                   for(int j = 0; j < filesChilds.getLength(); j++){
                      //and like above
                   }
             }
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM