簡體   English   中英

使用Java XPATH解析XML以獲取所有子節點及其數據

[英]Parsing XML to get all child nodes and their data using Java XPATH

嗨,我有解析XML並獲取所有子節點以及節點<Employees></Employees>

我有像這樣的xml:

<?xml version="1.0"?>
<Employees>
    <Employee emplid="1111" type="admin">
        <firstname>test1</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>johnwatson@sh.com</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>sherlock@sh.com</email>
    </Employee>
</Employees>

我需要像這樣的回應

<Employee emplid="1111" type="admin">
        <firstname>test1</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>johnwatson@sh.com</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>sherlock@sh.com</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
        johnwatson@sh.com

我已經使用過/ Employees / *這樣的表達式,但是它不起作用

有人可以幫助我嗎?

這可能是XSLT轉換:

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

如果您想將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));
     }

首先,如果要匹配每個Employee ,理想情況下,您的XPath表達式應該是Employee而不是/Employees/* 如果您知道標簽名稱,則也不需要XPath,只需執行xmlDocument.getElementsByTagName("Employee")

如果要將節點序列化為String,則可以使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM