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

发布于 2024-10-09 12:14:15 字数 1940 浏览 1 评论 0原文

给定一个如下所示的 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>

我想打印“key”属性和元素的值,因此它看起来像这样:

agentType = STANDARD
DestinationTransferStates = 
AgentStatusPublishRate = 300
agentVersion = f000-703-GM2-20101109-1550
CommandTimeUTC = 2010-12-24T02:25:43Z
PublishTimeUTC = 2010-12-24T02:26:09Z
queueManager = AGENTQMGR

我能够使用此代码毫无问题地打印节点值:

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()); 
}

我可以通过更改 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()); 
}

似乎有一种方法可以在单个 evaluate 中获取这两个值。我总是可以评估两个 NodeList 并使用公共索引迭代它们,但我不确定它们是否保证以相同的顺序返回。任何建议表示赞赏。

Given an xml document that looks like the following:

<?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>

I want to print the values of the "key" attribute and the element so it looks like this:

agentType = STANDARD
DestinationTransferStates = 
AgentStatusPublishRate = 300
agentVersion = f000-703-GM2-20101109-1550
CommandTimeUTC = 2010-12-24T02:25:43Z
PublishTimeUTC = 2010-12-24T02:26:09Z
queueManager = AGENTQMGR

I'm able to print the node values with no problem using this code:

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()); 
}

And I can print the values of the "key" attribute by changing the xpath expression and the node methods as follows:

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()); 
}

It seems like there would be a way to get at both of these values in a single evaluate. I could always evaluate two NodeLists and iterate through them with a common index but I'm not sure they are guaranteed to be returned in the same order. Any suggestions appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-10-16 12:14:15

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);
}

有关更多信息,请参阅 getTextContent()。我希望这会对您有所帮助。

What about getTextContent()? This should do the work.

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);
}

For further informations please see the javadoc for getTextContent(). I hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文