====================================这个是我的XML==================================== <?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:syncOrderRelation xmlns:ns2="http://impl.sp.superet.aspire.com">
<ns2:request>
<ns1:demandCode xmlns:ns1="http://model.sp.superet.aspire.com/xsd">
222
</ns1:demandCode>
</ns2:request>
</ns2:syncOrderRelation>
</soapenv:Body>
</soapenv:Envelope> ====================================我的解析方法===================================
InputSource in = new InputSource(new ByteArrayInputStream(strBack.getBytes()));//strBack是上面XML的字符串 String类型
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
try
{ Document pv_doc = dfactory.newDocumentBuilder().parse(in);
NodeIterator nl = XPathAPI.selectNodeIterator(pv_doc, "/soapenv:Envelope/soapenv:Body/ns2:syncOrderRelation/ns2:request/ns1:demandCode/text()");
System.out.println(nl.nextNode().getNodeValue()); }
catch (Exception e) {e.printStackTrace();} ====================================结果会报错:================================ org.apache.xpath.domapi.XPathStylesheetDOM3Exception: 前缀必须解析为名称空间:ns2
at org.apache.xpath.compiler.XPathParser.errorForDOM3(XPathParser.java:653)
at org.apache.xpath.compiler.Lexer.mapNSTokens(Lexer.java:638)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:265)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:96)
at org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:110)
at org.apache.xpath.XPath. <init>(XPath.java:176)
at org.apache.xpath.XPathAPI.eval(XPathAPI.java:232)
at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:126)
at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:106)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:syncOrderRelation xmlns:ns2="http://impl.sp.superet.aspire.com">
<ns2:request>
<ns1:demandCode xmlns:ns1="http://model.sp.superet.aspire.com/xsd">
222
</ns1:demandCode>
</ns2:request>
</ns2:syncOrderRelation>
</soapenv:Body>
</soapenv:Envelope> ====================================我的解析方法===================================
InputSource in = new InputSource(new ByteArrayInputStream(strBack.getBytes()));//strBack是上面XML的字符串 String类型
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
try
{ Document pv_doc = dfactory.newDocumentBuilder().parse(in);
NodeIterator nl = XPathAPI.selectNodeIterator(pv_doc, "/soapenv:Envelope/soapenv:Body/ns2:syncOrderRelation/ns2:request/ns1:demandCode/text()");
System.out.println(nl.nextNode().getNodeValue()); }
catch (Exception e) {e.printStackTrace();} ====================================结果会报错:================================ org.apache.xpath.domapi.XPathStylesheetDOM3Exception: 前缀必须解析为名称空间:ns2
at org.apache.xpath.compiler.XPathParser.errorForDOM3(XPathParser.java:653)
at org.apache.xpath.compiler.Lexer.mapNSTokens(Lexer.java:638)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:265)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:96)
at org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:110)
at org.apache.xpath.XPath. <init>(XPath.java:176)
at org.apache.xpath.XPathAPI.eval(XPathAPI.java:232)
at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:126)
at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:106)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货