java - 如何使用 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 的值时犯了什么错误?请帮忙。
解决方案
您可以使用@*[name()='xlink:label']
. @xlink:label
也切换到@*[local-name()='label']
应该做的伎俩。
推荐阅读
- macos - 无法在我的 macOS Catalina 上安装 CPLEX 12.9
- kubernetes - kubectl exec ... /bin/bash 吃控制-p?
- delphi - 如何在执行“TRead.ReadProp”过程之前将正确类型的对象加载到集合中
- algorithm - 如何根据进度值计算圆上的位置?
- html - CSS,调整窗口大小时按钮消失
- blockchain - 区块链出块时间
- swift - SwiftUI 无法从下载地址获取图片
- php - 通过网络服务器上的PHP问题下载文件
- cross-compiling - 如何在带有 bazel 的 x64 机器上为 aarch64 交叉编译 C++
- vba - 如何确保不显示 microsoft project vba 中的“详细信息”任务窗格