首页 > 解决方案 > 如何使用 XPath 读取 xlink:label 值

问题描述

我需要从 XML 获取 xlink:label value(ASSET_1)。

<MESSAGE xmlns:xlink="http://www.w3.org/1999/xlink">
<ABOUT_VERSIONS>
<ABOUT_VERSION SequenceNumber="1"  xlink:label="ASSET_1" >
<CreatedDatetime>2015-08-24T09:30:47Z</CreatedDatetime>
<DataVersionName>Purchase Example</DataVersionName>
</ABOUT_VERSION>
</ABOUT_VERSIONS>
</MESSAGE>

我正在尝试的Java代码如下

XPathFactory xpf = XPathFactory.newInstance();            
XPath xPath = xpf.newXPath();

XPathExpression pathExpression = xPath.compile("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION");   
InputSource inputSource = new InputSource("C:/Sample.xml");  
NodeList Nodes = (NodeList) xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION", inputSource, XPathConstants.NODESET);

System.out.println("SequenceNumber:: "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@SequenceNumber", inputSource, XPathConstants.NODE));
System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@xlink:label", inputSource, XPathConstants.NODE));
OutPut
SequenceNumber:: SequenceNumber="1"

null

我在提取 xlink:label 的值时犯了什么错误?请帮忙。

标签: javaxmlxlink

解决方案


您可以使用@*[name()='xlink:label']. @xlink:label也切换到@*[local-name()='label']应该做的伎俩。


推荐阅读