Xpath 似乎不起作用。我已经尝试了一些东西,但似乎没有任何效果。我究竟做错了什么?
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(result)));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//cm:URL/@value");
String msg = expr.evaluate(doc, XPathConstants.STRING).toString();
logger.debug(msg);
我拥有的 XML 如下所示:
<ItemXML xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:ns2="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema">
<DOCUMENTS SCA_DATE="#" SCA_NR="#" cm:PID="#" xmlns:cm="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema" xmlns:ns1="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cm:properties type="#">
<cm:lastChangeUserid value="#"/>
<cm:lastChangeTime value="#"/>
<cm:createUserid value="#"/>
<cm:createTime value="#"/>
<cm:semanticType value="#"/>
<cm:ACL name="#"/>
<cm:lastOperation name="#" value="#"/>
</cm:properties>
<ns1:CONTRACTS AM="#"/>
<ns1:BASE cm:PID="#" cm:partNumber="#">
<cm:properties type="item" xsi:type="#">
<cm:lastChangeUserid value="#"/>
<cm:lastChangeTime value="#"/>
<cm:createUserid value="#"/>
<cm:createTime value="#"/>
<cm:semanticType value="#"/>
<cm:ACL name="#"/>
<cm:lastOperation name="#" value="#"/>
</cm:properties>
我希望将cm:URL --> https://testurl.com的值保存为字符串。重要提示:无论 xml 结构如何,Xpath 都应该找到该值。
肥皂起泡泡
宝慕林4294392
繁星淼淼
相关分类