[英]Get values from XPath
I need to get some values from this xml document: 我需要从此xml文档中获取一些值:
<node id="A." label="General Literature">
<isComposedBy>
<node id="A.0" label="GENERAL">
<isComposedBy>
<node label="Biographies/autobiographies"/>
<node label="Conference proceedings"/>
<node label="General literary works (e.g., fiction, plays)"/>
</isComposedBy>
</node>
<node id="A.1" label="INTRODUCTORY AND SURVEY"/>
<node id="A.2" label="REFERENCE (e.g., dictionaries, encyclopedias, glossaries)"/>
<node id="A.m" label="MISCELLANEOUS"/>
</isComposedBy>
</node>
In Java how can i select only the nodes with the attributes id
and label
and then get the values of those attributes? 在Java中,我如何仅选择具有
id
和label
属性的节点,然后获取这些属性的值? I've tried with XPath using this expression: 我已经尝试过使用XPath使用以下表达式:
XPathExpression expr = path.compile("//*/@id | /@label");
But this is not returning what i want. 但是,这并没有返回我想要的。
Use XPathExpression expr = path.compile("//*[@id and @label]/(@id | /@label)");
使用
XPathExpression expr = path.compile("//*[@id and @label]/(@id | /@label)");
with XPath 2.0 or XPathExpression expr = path.compile("//*[@id and @label]/@id | //*[@id and @label]/@label");
与XPath 2.0或
XPathExpression expr = path.compile("//*[@id and @label]/@id | //*[@id and @label]/@label");
with XPath 1.0. XPath 1.0。
Using //node[@id and @label]
will get all node
elements having id
and label
attributes. 使用
//node[@id and @label]
将获得所有具有id
和label
属性的node
元素。 Then you would need to loop over the nodes to get their attribute values. 然后,您将需要遍历节点以获取其属性值。 An example using DOM:
使用DOM的示例:
for(int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attributes = node.getAttributes();
System.out.println(attributes.getNamedItem("id").getTextContent());
System.out.println(attributes.getNamedItem("label").getTextContent());
}
尝试这种方式:
XPathExpression expr = path.compile("//*/*[@id and @label]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.