Java 节点迭代元素的映射

Java 节点迭代元素的映射,java,dom,map,dom4j,Java,Dom,Map,Dom4j,Node来处理xml文件,我在映射类似结构时遇到了一个问题。 我遵循了文件结构: <Party> <Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier> <Address> </Address> <Contact> <ContactInfo> <Key>IPS</Key>

Node来处理xml文件,我在映射类似结构时遇到了一个问题。 我遵循了文件结构:

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

1113ddbed7b54890abfe2f8c9754d689
IPS
无效的
关键二
1234
(...)
我的目标是为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);
          }
    }
//父节点是父元素
Node parentDocument=parentNode。选择SingleNode(“ContactInfo”);
列出节点键=联系人节点。选择节点(“键”);
列出节点值=contactNode。选择节点(“值”);
对于(inti=0;i我认为dom4j+将解决这个问题,您只需要添加依赖项

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

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

杰克森
杰克森
1.1.6

干杯。

以下是一个完整的工作示例:

马文:波姆

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

1113ddbed7b54890abfe2f8c9754d689
IPS
无效的
关键二
1234

将从哪个节点返回。选择SingleNode(//Party/Contact/ContactInfo/[Key='keytower']);?如果只有keyTwo?或pair keyTwo1234?//Party/Contact/ContactInfo/==>ContactInfo可以帮我查询一下吗?我会很感激xml文件的大小?不是很大,大约是1-200行如果你有一个大于数兆字节的大文件,你可以将Xpath+Dom4j和流节点结合起来,每个解析的节点都应该分离处理后从dom树释放ram。
<?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>