Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Java 在JAXP中使用XPath检索XML节点和节点属性的值_Java_Xml_Xpath_Jaxp - Fatal编程技术网

Java 在JAXP中使用XPath检索XML节点和节点属性的值

Java 在JAXP中使用XPath检索XML节点和节点属性的值,java,xml,xpath,jaxp,Java,Xml,Xpath,Jaxp,给定如下所示的xml文档: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="agentType">STANDARD</entry> <entry key="DestinationTransferSt

给定如下所示的xml文档:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="agentType">STANDARD</entry>
    <entry key="DestinationTransferStates"></entry>
    <entry key="AgentStatusPublishRate">300</entry>
    <entry key="agentVersion">f000-703-GM2-20101109-1550</entry>
    <entry key="CommandTimeUTC">2010-12-24T02:25:43Z</entry>
    <entry key="PublishTimeUTC">2010-12-24T02:26:09Z</entry>
    <entry key="queueManager">AGENTQMGR</entry>
</properties>
使用以下代码,我可以毫无问题地打印节点值:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//properties/entry/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getNodeValue()); 
}
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression expr=xpath.compile(“//properties/entry/text()”;
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i
我可以通过如下更改xpath表达式和节点方法来打印“key”属性的值:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//properties/entry");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getAttributes().getNamedItem("key").getNodeValue()); 
}
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression expr=xpath.compile(“//properties/entry”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i

似乎有一种方法可以在一次
评估中获得这两个值。我总是可以评估两个节点列表,并用一个公共索引遍历它们,但我不确定它们是否保证以相同的顺序返回。感谢您的建议。

关于
getTextContent()
呢?这应该可以完成工作

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
{
    Node currentItem = nodes.item(i);
    String key = currentItem.getAttributes().getNamedItem("key").getNodeValue();
    String value = currentItem.getTextContent();

    System.out.printf("%1s = %2s\n", key, value);
}
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i

有关更多信息,请参阅javadoc。我希望这将对您有所帮助。

复制、粘贴、工作完美。谢谢我将进一步了解getTextContent()。看起来jaxp的xml代码非常混乱,您是否对其他类型的xml API持开放态度?一旦jaxp代码正常工作,我就用XSLT编写了它。这是一个非常干净(看起来像HTML与一些添加的标记),但需要永远呈现。下一步,我将尝试使用带音译的XSLT。当然欢迎其他建议。
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
{
    Node currentItem = nodes.item(i);
    String key = currentItem.getAttributes().getNamedItem("key").getNodeValue();
    String value = currentItem.getTextContent();

    System.out.printf("%1s = %2s\n", key, value);
}