要使用Java中的命名空间进行XPath查询,我相信您需要执行以下操作:
XPath xpath = XPathFactory.newInstance().newXPath();xpath.setNamespaceContext(new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { switch (prefix) { case "env": return "http://schemas.xmlsoap.org/soap/envelope/"; case "ns2": return "http://abc.examples.com/"; } return XMLConstants.NULL_NS_URI; } @Override public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException("Not supported yet."); } @Override public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException("Not supported yet."); }});Node getNewTokenResp = (Node) xpath.evaluate("/env:Envelope/env:Body/ns2:getNewTokenResponse", document, XPathConstants.NODE);您还需要先调用
.setNamespaceAware(true);,
documentBuilderFactory然后再创建
documentBuilder来解析文档。



