我正在尝试使用Java中的XPath获取标签“ fax”的值(请参见下面的示例XML)。
我决定尝试获取用于“业务”的节点,并逐步通过调试器以查看是否可以看到标签……似乎不起作用……我正在使用的代码片段是:
String path =
"/locationDetailResponse/locationInfo/locationBusinessList/business"
XPath xPath = XPathFactory.newInstance().newXPath();
Element userElement = (Element) xPath.evaluate(path, documentObject,
XPathConstants.NODE);
documentObject包含org.w3c.dom.Document对象
<location>
<locationInfo>
<warehouseId>99</warehouseId>
<nearByLocations>
<location>
<name>Morganton, NC</name>
<url>morganton-nc-hvac</url>
</location>
<location>
<name>Statesville, NC</name>
<url>statesville-nc-plumbing</url>
</location>
</nearByLocations>
<locationBusinessList>
<business>
<id>123</id>
<fax>(800) 555-1212</fax>
</business>
<business>
<id>456</id>
<fax>(800) 666-2323</fax>
</business>
</locationBusinessList>
</locationInfo>
</location>
我应该使用正确的XPath表达式的任何想法吗?
答案 0 :(得分:0)
您可以尝试在行首将/
更改为//
,
或使用local-name:
//*[local-name()='location']/*[local-name()='locationInfo']/*[local-name()='locationBusinessList']/*[local-name()='business']