2014-09-03 90 views
0

dom4j.Node處理xml文件,並且我在Map類似的結構中遇到了問題。 我按照文檔結構:dom4j.Node元素的迭代映射

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

但我不知道這是好辦法,特別是如果鍵和值的列表將被正確排序。

+0

什麼的大小xml文件? – 2014-09-03 16:54:52

+0

不大,如1-2行 – Mazeryt 2014-09-03 16:57:12

+0

如果你有一個大文件>一些兆字節,你可以結合Xpath + Dom4j和流節點,每個解析的節點應該從dom樹中分離後處理釋放內存。 – 2014-09-03 17:01:59

回答

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主營:

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

乾杯。

+0

什麼節點將從.selectSingleNode(「// Party/Contact/ContactInfo/[Key ='keyTwo']」)返回。 ?如果只有 keyTwo?或對 keyTwo ? – Mazeryt 2014-09-03 14:07:25

+0

//派對/聯繫/聯繫信息/ ==>聯繫人信息 – 2014-09-03 14:08:42

+0

這取決於你如何格式化你的Xpath查詢 – 2014-09-03 14:09:35