Extensive XML parsing in java

Hi, I want to parse a rather strange xml that looks like this:

<foo>
    <foo1>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
    </foo1>
</foo>

<foo>
    <foo1>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
        <foo2></foo2>
    </foo1>
</foo>

I cannot get values ​​from foo2 when I used this tutorial:

http://javarevisited.blogspot.com/2011/12/parse-xml-file-in-java-example-tutorial.html

But this does not work if we have the same parameters as foo2

+1
source share
4 answers

// Normalize your XML, then create Documentthat XML, and then execute the code below

NodeList nList = doc.getElementsByTagName("foo");
        for (int temp = 0; temp < nList.getLength(); temp++) { // loop for foo
            Node nodeTable = nList.item(temp);
            Element xmlFoo = (Element) nodeTable;
            if (nodeTable.getNodeType() == Node.ELEMENT_NODE) {
            NodeList nodeListFoo1 = xmlFoo.getElementsByTagName("foo1");
            for (int i = 0; i < nodeListFoo1.getLength(); i++) { // loop for foo1
                Node nodefoo2 = nodeListFoo1.item(i);
                Element elementfoo1 = (Element) nodefoo2;
                // Collect foo2 
                NodeList nodeListfoo2Name = elementfoo1.getElementsByTagName("foo2");
                for (int j = 0; j < nodeListfoo2Name.getLength(); j++) { //// loop for foo2
                    Node nodefoo2Name = nodeListfoo2Name.item(j);
                    Element elementfoo2Name = (Element) nodefoo2Name;
                    //TODO: write code here, what you want to collect from foo2
                    }
                }
            }
        }
0
source

This is because you must have a parent element to hold all other elements, for example, the example below:

<parentFoo>
    <foo>
        <foo1>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
        </foo1>
    </foo>

    <foo>
        <foo1>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
           <foo2></foo2>
        </foo1>
    </foo>
</parentFoo>
+1
source

jdom2. .

XML- Document. xml.

    SAXBuilder reader = new SAXBuilder();
    Document doc = null;
    try
    {
        doc = reader.build(new File("Path to XML")); //also string is possible
    }
    catch (JDOMException | IOException e)
    {
        e.printStackTrace();
    }

. . , , , childCount != 0, . , .

for (Element e : doc.getRootElement().getChildren())
    {
         // do something with the childs here for example
         e.getText(); //would be "" in your case
         for(Element child : e.getChildren()){
             //do something with the child child
         }
    }

. attribute getName() .

DOM.

0

, JAXB - . - XSD xml. XSD JAX-B, Netbeans Java.

Then you can use the standard JAX-B API to sort and cancel the route.

-1
source

Source: https://habr.com/ru/post/1527986/


All Articles