DOM4J xpath-select 带命名空间的冒号节点
DOM4J xpath-select colon node with namespace
鉴于xml内容如下;
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CreateResponse xmlns="http://gtestserver/"><CustomerId>8124138</CustomerId><InternalResponseCode>Success</InternalResponseCode><ResponseCode>0</ResponseCode><ResponseDate>2016-08-31T07:57:22.7760577Z</ResponseDate><ResponseDescription>Success</ResponseDescription><UserId>7424876375</UserId></CreateResponse></s:Body></s:Envelope>
我需要获取节点:CustomerId,但我无法正确获取它,我在这里使用的代码:
Document document = new SAXReader().read(new ByteArrayInputStream(xmlfile.getBytes("UTF-8")));
Node foundnode = null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if (namespace != null) {
DefaultXPath defaultXPath = new DefaultXPath(xpath);
Map<String, String> namespaces = new TreeMap<String, String>();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
}
foundnode = document.selectSingleNode("/ns:s:Envelope/ns:s:Body/ns:CreateResponse/ns:CustomerId");
然后抛出以下异常:
org.dom4j.InvalidXPathException: Invalid XPath expression: /ns:s:Envelope/ns:s:Body/ns:CreateResponse/ns:CustomerId Unexpected ':'
at org.dom4j.xpath.DefaultXPath.parse(DefaultXPath.java:360)
at org.dom4j.xpath.DefaultXPath.<init>(DefaultXPath.java:59)
at com.github.becauseQA.xml.DOM4JUtils.getNodes(DOM4JUtils.java:152)
at
我知道这是由这里使用的 xpath 引起的,但我不知道我应该在这里选择哪个 xpath,你可以看到节点有 : ,所以当使用带有 ns: 的命名空间时,对于每个节点,它不能解析它。任何人都知道如何在节点名称中获取具有命名空间和特殊字符的节点?谢谢.
您可以尝试以下方法:
String xml = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><CreateResponse xmlns=\"http://gtestserver/\"><CustomerId>8124138</CustomerId><InternalResponseCode>Success</InternalResponseCode><ResponseCode>0</ResponseCode><ResponseDate>2016-08-31T07:57:22.7760577Z</ResponseDate><ResponseDescription>Success</ResponseDescription><UserId>7424876375</UserId></CreateResponse></s:Body></s:Envelope>";
Document document = new SAXReader().read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Node foundnode = null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
foundnode = document.selectSingleNode("//*[local-name()='CustomerId']");
System.out.println(foundnode.getText());
这个 returns : 8124138
鉴于xml内容如下;
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CreateResponse xmlns="http://gtestserver/"><CustomerId>8124138</CustomerId><InternalResponseCode>Success</InternalResponseCode><ResponseCode>0</ResponseCode><ResponseDate>2016-08-31T07:57:22.7760577Z</ResponseDate><ResponseDescription>Success</ResponseDescription><UserId>7424876375</UserId></CreateResponse></s:Body></s:Envelope>
我需要获取节点:CustomerId,但我无法正确获取它,我在这里使用的代码:
Document document = new SAXReader().read(new ByteArrayInputStream(xmlfile.getBytes("UTF-8")));
Node foundnode = null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
if (namespace != null) {
DefaultXPath defaultXPath = new DefaultXPath(xpath);
Map<String, String> namespaces = new TreeMap<String, String>();
namespaces.put("ns", namespace);
defaultXPath.setNamespaceURIs(namespaces);
}
foundnode = document.selectSingleNode("/ns:s:Envelope/ns:s:Body/ns:CreateResponse/ns:CustomerId");
然后抛出以下异常:
org.dom4j.InvalidXPathException: Invalid XPath expression: /ns:s:Envelope/ns:s:Body/ns:CreateResponse/ns:CustomerId Unexpected ':'
at org.dom4j.xpath.DefaultXPath.parse(DefaultXPath.java:360)
at org.dom4j.xpath.DefaultXPath.<init>(DefaultXPath.java:59)
at com.github.becauseQA.xml.DOM4JUtils.getNodes(DOM4JUtils.java:152)
at
我知道这是由这里使用的 xpath 引起的,但我不知道我应该在这里选择哪个 xpath,你可以看到节点有 : ,所以当使用带有 ns: 的命名空间时,对于每个节点,它不能解析它。任何人都知道如何在节点名称中获取具有命名空间和特殊字符的节点?谢谢.
您可以尝试以下方法:
String xml = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><CreateResponse xmlns=\"http://gtestserver/\"><CustomerId>8124138</CustomerId><InternalResponseCode>Success</InternalResponseCode><ResponseCode>0</ResponseCode><ResponseDate>2016-08-31T07:57:22.7760577Z</ResponseDate><ResponseDescription>Success</ResponseDescription><UserId>7424876375</UserId></CreateResponse></s:Body></s:Envelope>";
Document document = new SAXReader().read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Node foundnode = null;
Element rootElement = document.getRootElement();
String namespace = rootElement.getNamespaceURI();
foundnode = document.selectSingleNode("//*[local-name()='CustomerId']");
System.out.println(foundnode.getText());
这个 returns : 8124138