Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaXPath解析XML以获取所有子节点及其数据_Java_Xml_Xpath_Xml Parsing - Fatal编程技术网

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

使用JavaXPath解析XML以获取所有子节点及其数据,java,xml,xpath,xml-parsing,Java,Xml,Xpath,Xml Parsing,您好,我需要解析xml并获取所有子节点以及节点之间的数据和 我有如下xml: <?xml version="1.0"?> <Employees> <Employee emplid="1111" type="admin"> <firstname>test1</firstname> <lastname>Watson</lastname> <age>3

您好,我需要解析xml并获取所有子节点以及节点之间的数据

我有如下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>
  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);
因此,返回一个
节点列表
,您可以使用

     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));
     }
String expression=“/Employees/*”;
System.out.println(表达式);
NodeList元素=(NodeList)xPath.compile(expression.evaluate(xmlDocument,XPathConstants.NODESET);
对于(int i=0;i
首先,如果要匹配每个
员工
,理想情况下,XPath表达式应该是
员工
而不是
/Employees/*
。如果知道标记名,也不需要XPath,只需执行
xmlDocument.getElementsByTagName(“员工”)


如果要将节点序列化为字符串,可以使用
转换器
,如下所示:

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);
}
Transformer t=TransformerFactory.newTransformer();
NodeList nodes=xmlDocument.getElementsByTagName(“员工”);
对于(int i=0;i
XPath表达式选择根元素的所有子元素是正确的。但是,要将节点序列化回标记字符串,需要使用DOM Level 3加载和保存API或默认转换器。因此,我们不能使用简单的XPath吗?如果您有任何工作代码可以共享吗?XPath 1.0和2.0不序列化节点,no、 在XPath 3.0中,您可以使用,例如
/Employees/*/serialize(.)
来获取字符串序列。但是Oracle Java JRE不支持XPath 3.0,并且您使用的API也不是为XPath 2.0或3.0设计的。
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);
}