我是XPath的新手,遇到以下问题:
我有一个从 web 服务接收数据的 Java 方法,这些数据位于 XML 文档中,因此我必须使用 XPath 在此 XML 结果文档中获取特定值。
特别是我知道这是我的 Web 服务(Web 服务响应)提供的整个 XML 输出:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<getConfigSettingsResponse xmlns="http://tempuri.org/">
<getConfigSettingsResult><![CDATA[<root>
<status>
<id>0</id>
<message></message>
</status>
<drivers>
<drive id="tokenId 11">
<shared-secret>Shared 11</shared-secret>
<encoding>false</encoding>
<compression />
</drive>
<drive id="tokenId 2 ">
<shared-secret>Shared 2 </shared-secret>
<encoding>false</encoding>
<compression>false</compression>
</drive>
</drivers>
</root>]]></getConfigSettingsResult>
</getConfigSettingsResponse>
</s:Body>
</s:Envelope>
现在在一个 Java 类中,我执行以下操作:
XPath xPath; // An utility class for performing XPath calls on JDOM nodes
Element objectElement; // An XML element
//xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult");
try {
// XPath selection:
xPath = XPath.newInstance("s:Envelope/s:Body");
xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
objectElement = (Element) xPath.selectSingleNode(documentXML);
if (objectElement != null) {
result = objectElement.getValue();
System.out.println("RESULT:");
System.out.println(result);
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
打印结果变量的内容的结果是这个输出:
RESULT:
<root>
<status>
<id>0</id>
<message></message>
</status>
<drivers>
<drive id="tokenId 11">
<shared-secret>Shared 11</shared-secret>
<encoding>false</encoding>
<compression />
</drive>
<drive id="tokenId 2 ">
<shared-secret>Shared 2 </shared-secret>
<encoding>false</encoding>
<compression>false</compression>
</drive>
</drivers>
</root>
现在我的问题是我只想访问0标签的内容,所以我希望(在这种情况下)我的结果变量必须包含0值。
但我不能,我尝试使用以下命令更改以前的 XPath 选择:
xPath = XPath.newInstance("s:Envelope/s:Body/s:status/s:id");
但是这样做我得到我的objectElement是null
为什么?我错过了什么?我该怎么做才能使 mu 结果变量包含id标签的内容?
肿瘤坏死因子
安德烈亚