猿问

如何使用 XPath 读取 xlink:label 值

我需要从 XML 获取 xlink:label 值(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 的值时犯了什么错误?请帮忙。


精慕HU
浏览 247回答 1
1回答

HUX布斯

您可以使用@*[name()='xlink:label']代替@xlink:label.&nbsp;也切换到@*[local-name()='label']应该可以解决问题。
随时随地看视频慕课网APP

相关分类

Java
我要回答