Search code examples
javadomdictionarydom4j

dom4j.Node iterate maps of elements


dom4j.Node to process xml files, and I had a problem with Map-similar structures. I have followed document structure:

<Party>
    <Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
    <Address>
    </Address>
<Contact>
    <ContactInfo>
        <Key>IPS</Key>
        <Value>null</Value>
        <Key>keyTwo</Key>
        <Value>1234</Value>
        (...)
    </ContactInfo>
</Contact>
</Party>

And my goal is to get value for keyTwo element. How to get those element with flexible, non hardcoded way?

My first idea was something like that:

    //parent node is father element
    Node parentDocument = parentNode.selectSingleNode("ContactInfo");
    List<Node> nodesKeys = contactNode.selectNodes("Key");
    List<Node> nodesValues = contactNode.selectNodes("Value");

    for(int i=0; i<nodesKeys.size(); i++){

          if(nodesKeys.get(i).selectSingleNode("Key").equals("keyTwo")){
              return nodesValues.get(i);
          }
    }

But I'm not sure if it is good approach, especially if list of keys and values will be correctly ordered.


Solution

  • Here is a complete working example:

    Maven : PoM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.sofrecom</groupId>
        <artifactId>XmlProcessing</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <dependencies>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>jaxen</groupId>
                <artifactId>jaxen</artifactId>
                <version>1.1.6</version>
            </dependency>
        </dependencies>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    </project>
    

    Java Main:

    package xmlprocessing;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    
    /**
     *
     * @author z.benrhouma
     */
    public class Main {
    
        public static void main(String... args) {
    
            try {
                Document document =  parse("src/main/resources/file.xml");
                Node node = document.selectSingleNode( "//Party/Contact/ContactInfo/Key[text()='keyTwo']/following-sibling::Value[1]");
                System.out.println(node.getText());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    
        }
        public static Document parse(String path) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(path);
            return document;
        }
    }
    

    XML file :

    <?xml version="1.0" encoding="UTF-8"?>
    <Party>
        <Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
        <Address>
        </Address>
        <Contact>
            <ContactInfo>
                <Key>IPS</Key>
                <Value>null</Value>
                <Key>keyTwo</Key>
                <Value>1234</Value>
    
            </ContactInfo>
        </Contact>
    </Party>