我无法获得以下 xpath 查询的结果。
代码如下
FileInputStream file = new FileInputStream(new File("D:/reservationresponse.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath1 = factory.newXPath();
NamespaceContext ctx = new NamespaceContext() {
public String getNamespaceURI(String prefix) {
String uri = null;
if (prefix.equals("SOAP-ENV"))
uri = "http://schemas.xmlsoap.org/soap/envelope/";
else if (prefix.equals("header"))
uri = "http://wsi.nat.qt.com/2005/06/StandardHeader/";
else if (prefix.equals("S18"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01";
else if (prefix.equals("S18"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01";
else if (prefix.equals("SOAP-ENC"))
uri = "http://schemas.xmlsoap.org/soap/encoding/";
else if (prefix.equals("xsi"))
uri = "http://www.w3.org/2001/XMLSchema-instance";
else if (prefix.equals("xsd"))
uri = "http://www.w3.org/2001/XMLSchema";
else if (prefix.equals("S2"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/CCM/Agreements";
else if (prefix.equals("S16"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/MTOSI";
else if (prefix.equals("S8"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/MTOSI/Structs";
else if (prefix.equals("S11"))
uri = "http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/NonCCM";
//uri = null
System.out.println(uri);
return uri;
}
public Iterator getPrefixes(String val) {
return null;
}
// Dummy implemenation - not used!
public String getPrefix(String uri) {
return null;
}
};
xpath1.setNamespaceContext(ctx);
XPathExpression expr = xpath1.compile("//SOAP-ENV:Envelope/SOAP- ENV:Body/S18:reserveResourceResponse/header:standardHeader/header:e2e/header:E2EDATA/text() ");
Object result = expr.evaluate(xmlDocument);
System.out.println("********** &&&&&&&&&" + result);
用于解析的 XML: 这是我要解析的 xml
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:header="http://wsi.nat.qt.com/2005/06/StandardHeader/">
<SOAP-ENV:Body>
<S18:reserveResourceResponse
xsi:schemaLocation="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01 ManageResourceServiceInterface.20090501.xsd"
xmlns:S18="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01"
xmlns:S2="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/CCM/Agreements"
xmlns:S16="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/MTOSI"
xmlns:S8="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/MTOSI/Structs"
xmlns:S11="http://capabilities.nat.qt.com/xsd/ManageResourceService/2009/05/01/NonCCM">
<header:standardHeader>
<header:e2e>
<header:E2EDATA>10=2pzmw0etdv,1=2pzmwi65s6,7=apl08929imbwa2,6=PCK002069,4=RoBTESB-default,9=wlsuser,15=RoBTESB,8=UNKNOWN,1.2=APP10865:2pzmwi65s6:,1.1=APP10865:2pzmw0esr0:,16=LIMS
</header:E2EDATA>
</header:e2e>
结果为空