2015-10-02 51 views
0

您好我有要求像解析XML,並得到所有子節點有節點<Employees></Employees>解析XML得到所有子節點和他們的數據使用Java XPATH

之間的數據我有XML這樣的:

<?xml version="1.0"?> 
<Employees> 
    <Employee emplid="1111" type="admin"> 
     <firstname>test1</firstname> 
     <lastname>Watson</lastname> 
     <age>30</age> 
     <email>[email protected]</email> 
    </Employee> 
    <Employee emplid="2222" type="admin"> 
     <firstname>Sherlock</firstname> 
     <lastname>Homes</lastname> 
     <age>32</age> 
     <email>[email protected]</email> 
    </Employee> 
</Employees> 

我需要一個像

<Employee emplid="1111" type="admin"> 
     <firstname>test1</firstname> 
     <lastname>Watson</lastname> 
     <age>30</age> 
     <email>[email protected]</email> 
    </Employee> 
    <Employee emplid="2222" type="admin"> 
     <firstname>Sherlock</firstname> 
     <lastname>Homes</lastname> 
     <age>32</age> 
     <email>[email protected]</email> 
    </Employee> 

響應我曾嘗試下面的代碼

FileInputStream file = new FileInputStream(new File("E:\\test.xml")); 

     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 

     DocumentBuilder builder = builderFactory.newDocumentBuilder(); 

     Document xmlDocument = builder.parse(file); 

     XPath xPath = XPathFactory.newInstance().newXPath(); 

     System.out.println("*************************"); 
     String expression = "/Employees/*"; 
     System.out.println(expression); 
     String email = xPath.compile(expression).evaluate(xmlDocument); 
     System.out.println(email); 

,但我越來越喜歡

test1 
     Watson 
     30 
     [email protected] 

迴應,我已經使用表達喜歡/員工/ *,但它不工作

誰能幫我做這個?

+0

的XPath表達式是正確的選擇根元素的所有子元素。但是,要將節點序列化回一串標記,需要使用DOM Level 3 Load和Save API或默認轉換器。 –

+0

所以我們不能使用簡單的xpath?如果你有任何工作代碼可以分享? –

+0

XPath 1.0和2.0不會序列化節點,不,在XPath 3.0中,您可以使用http://www.w3.org/TR/xpath-functions-30/#func-serialize,例如, '/ Employees/*/serialize(。)'得到一串字符串。但Oracle Java JRE不支持XPath 3.0,您使用的API不是專爲XPath 2.0或3.0而設計的。 –

回答

0

這將是一個可能的XSLT轉換:

<xsl:template match="Employees"> 
    <xsl:copy-of select = "Employee" /> 
</xsl:template> 
0

如果你想序列化DOM節點字符串中使用例如

import org.w3c.dom.bootstrap.DOMImplementationRegistry; 
import org.w3c.dom.Document; 
import org.w3c.dom.ls.DOMImplementationLS; 
import org.w3c.dom.ls.LSSerializer; 

... 

DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 

DOMImplementationLS impl = 
    (DOMImplementationLS)registry.getDOMImplementation("LS"); 

LSSerializer writer = impl.createLSSerializer(); 
String str = writer.writeToString(node); 

所以返回NodeList你可以使用

 String expression = "/Employees/*"; 
    System.out.println(expression); 
    NodeList elements = (NodeList)xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); 
    for (int i = 0; i < elements.getLength(); i++) 
    { 
     System.out.println(writer.writeToString(element.item(i)); 
    } 
0

首先,如果你想給每個Employee匹配,最好你的XPath表達式應該是Employee/Employees/*。如果您知道標籤名稱,則您也不需要XPath,只需執行xmlDocument.getElementsByTagName("Employee")即可。

如果你想序列化節點爲字符串,您可以使用Transformer,是這樣的:

Transformer t = TransformerFactory.newTransformer(); 
NodeList nodes = xmlDocument.getElementsByTagName("Employee"); 
for(int i = 0; i < nodes.getLength(); i++) { 
    StringWriter sw = new StringWriter(); 
    t.transform(new DOMSource(nodes.item(i)), new StreamResult(sw)); 
    String serialized = sw.toString(); 
    System.out.println(serialized); 
}