简体   繁体   中英

parsing xml with w3c DOM and order of elements

I'm using java w3c DOM library for parsing XML document.

part of the XML document looks like :

<pin direction="output" id="out0" index="0" type="org.opencv.core.Mat">
    <property class="java.lang.String" name="tooltip"><![CDATA[, output image]]></property>
    <property class="java.util.UUID" name="uuid"><![CDATA[4a729579-e4f4-4e5c-8522-025701e19039]]></property>
    <property class="java.lang.Class" name="blockClass"><![CDATA[class org.obbb.model.Pin]]></property>
</pin>
<pin direction="output" id="out1" index="1" type="java.lang.String">
    <property class="java.lang.String" name="tooltip"><![CDATA[, filename]]></property>
    <property class="java.util.UUID" name="uuid"><![CDATA[88f616b6-4baf-423d-b2af-8df48ef25184]]></property>
    <property class="java.lang.Class" name="blockClass"><![CDATA[class org.obbb.model.Pin]]></property>
</pin>
<pin direction="output" id="out2" index="2" type="java.lang.String">
    <property class="java.lang.String" name="tooltip"><![CDATA[, file path]]></property>
    <property class="java.util.UUID" name="uuid"><![CDATA[1ca31982-6dea-4f48-8781-7f8194ba874a]]></property>
    <property class="java.lang.Class" name="blockClass"><![CDATA[class org.obbb.model.Pin]]></property>
</pin>

In this structure I need to parse the hash codes:

4a729579-e4f4-4e5c-8522-025701e19039

88f616b6-4baf-423d-b2af-8df48ef25184

1ca31982-6dea-4f48-8781-7f8194ba874a

My code for that is :

for (int i = 0; i < pinsLength; i++) {
    Node pin = pins.item(i);
    if ( pin.getNodeType() == Node.ELEMENT_NODE ) {
       Element pinElement = (Element)pin;
       String uuid = pinElement.getChildNodes().item(1).getTextContent();
    }
}

Will the hash code will always be saved in uuid variable since it's always second property element in the XML file?

I would not count on this. You can get some additional nodes between elements. For instance, someone might think adding a comment would be a good idea:

<pin direction="output" id="out1" index="1" type="java.lang.String">
    <property class="java.lang.String" name="tooltip"><![CDATA[, filename]]></property>
    <!-- Best UUID of all times -->
    <property class="java.util.UUID" name="uuid"><![CDATA[88f616b6-4baf-423d-b2af-8df48ef25184]]></property>
    <property class="java.lang.Class" name="blockClass"><![CDATA[class org.obbb.model.Pin]]></property>
</pin>

So, essentially this might work if your XML structure will stay the same forever, but I would not count on it. Also, hardcoding this may be hard to change later on.

What you can do instead is use XPath to retrieve data. See this question for quickstart:

How to read XML using XPath in Java

You need an XPath like:

//pin/property[@name='uuid']/text()

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