解析XML以使用Java XPATH获取所有子节点及其数据

时间:2015-10-02 10:37:17

标签: java xml xpath xml-parsing

您好我需要解析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 / * 这样的表达,但它不起作用

任何人都可以帮我这样做吗?

3 个答案:

答案 0 :(得分:0)

这将是一个可能的XSLT转换:

print("Total: ${}".format(total))

答案 1 :(得分: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));
     }

答案 2 :(得分:0)

首先,如果您想匹配每个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);
}