dom4j.Node迭代元素的映射

时间:2014-09-03 13:48:53

标签: java dom map dom4j

dom4j.Node处理xml文件,我遇到类似Map的结构问题。 我遵循了文档结构:

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

我的目标是获得keyTwo元素的价值。如何以灵活,非硬编码的方式获取这些元素?

我的第一个想法是这样的:

    //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);
          }
    }

但是我不确定它是否是好的方法,特别是如果键和值的列表将被正确排序。

2 个答案:

答案 0 :(得分:1)

这是一个完整的工作示例:

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文件:

<?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>

答案 1 :(得分:0)

我认为dom4j + Xpath会解决问题,您只需要添加jaxen依赖项。

// ContactInfo Element having key = 'keyTwo'
Node node = document.selectSingleNode( "//Party/Contact/ContactInfo[Key = 'keyTwo']");
// retrieve data from the selected node
String value = node... ;

Maven:

<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1.6</version>
</dependency>

干杯。