雖然我看到類似的帖子,我似乎無法解決這個問題(所以我手動解析XML,而不是使用XPath表達式)...這是我有XML ...爪哇Xpath名稱空間解析
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tnew="http://PR_Library/TNeWebService">
<soapenv:Header/>
<soapenv:Body>
<tnew:ApercuenRequest>
<tnew:OpcionCarga>0</tnew:OpcionCarga>
<tnew:Sucursal>1</tnew:Sucursal>
<tnew:Vendedor>8000</tnew:Vendedor>
<tnew:CanalConsulta>1</tnew:CanalConsulta>
<tnew:UsuarioCarga>8000</tnew:UsuarioCarga>
<tnew:Comercio>0</tnew:Comercio>
<tnew:FechaEvaluacion>20140109</tnew:FechaEvaluacion>
<tnew:ModeloDeEvaluacion></tnew:ModeloDeEvaluacion>
<tnew:IdEvaluacion>3694852</tnew:IdEvaluacion>
<tnew:Dudosa>0</tnew:Dudosa>
<tnew:Exceptuado>1</tnew:Exceptuado>
<tnew:TipoDocumento>1</tnew:TipoDocumento>
<tnew:NumeroDocumento>5881040</tnew:NumeroDocumento>
<tnew:Sexo>F</tnew:Sexo>
<tnew:Rol>5</tnew:Rol>
<tnew:RelacionLaboral>5</tnew:RelacionLaboral>
<tnew:TipoEmpleado>0</tnew:TipoEmpleado>
<tnew:FechaIngresoEmpleo></tnew:FechaIngresoEmpleo>
<tnew:AntiguedadLaboral>0</tnew:AntiguedadLaboral>
<tnew:IngresoMensualAnalista>0</tnew:IngresoMensualAnalista>
<tnew:IngresoMensual>6000</tnew:IngresoMensual>
<tnew:EstadoCivil>4</tnew:EstadoCivil>
<tnew:FechaDeNacimiento>19540423</tnew:FechaDeNacimiento>
<tnew:SituacionVivienda>3</tnew:SituacionVivienda>
<tnew:DomicilioProvincia></tnew:DomicilioProvincia>
<tnew:DomicilioLocalidad></tnew:DomicilioLocalidad>
<tnew:NivelEstudios></tnew:NivelEstudios>
<tnew:RelacionConTN>1</tnew:RelacionConTN>
<tnew:FechaAltaAbogado></tnew:FechaAltaAbogado>
<tnew:MarcaAbogado>N</tnew:MarcaAbogado>
<tnew:Cdi></tnew:Cdi>
<tnew:Campana></tnew:Campana>
</tnew:ApercuenRequest>
</soapenv:Body>
</soapenv:Envelope>
而且下面的Java代碼...
private XPathFactory xpathFactory = null;
private XPath xpath = null;
private DocumentBuilderFactory factory = null;
private DocumentBuilder builder= null;
private XPathExpression expr;
private Node nodo = null;
private Node nodoAux = null;
private NodeList result = null;
private NodeList nodeList = null;
...
try {
xpathFactory = XPathFactory.newInstance();
xpath = xpathFactory.newXPath();
factory = DocumentBuilderFactory.newInstance();
builder= factory.newDocumentBuilder();
NamespaceContext context = new NameSpaceNevada();
xpath.setNamespaceContext(context);
}
catch (Exception e) {
}
...
evaluarExpresion(documentoXML,"/soapenv:Envelope/soapenv:Body/tnew:ApercuenRequest/tnew:OpcionCarga")
private String evaluarExpresion(Document documentoXML, String expresion) throws Exception{
try {
expr = xpath.compile(expresion);
result = (NodeList)expr.evaluate(documentoXML, XPathConstants.NODESET);
System.out.println((result.item(0)!=null?result.item(0).getNodeName():"") + " ->>>> " + (result.item(0).getFirstChild()!=null?result.item(0).getFirstChild().getNodeValue():""));
return result.item(0).getFirstChild()!=null?result.item(0).getFirstChild().getNodeValue():null;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
,我似乎不能如您在XML看到它獲得「0」值...我已經嘗試了很多的東西...我會看...事情是,我可以解析它manualy,但笑話是通過XPath表達式,所以我可以保存代碼...
希望我讓我理解...
從NameSpaceNevada
對象的getNamespaceURI
方法如下:
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("No prefix provided!");
} else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return "http://univNaSpResolver/book";
} else if (prefix.equals("soapenv")) {
return "http://schemas.xmlsoap.org/soap/envelope/";
} else if (prefix.equals("tnew")) {
return "http://PR_Library/TNeWebService";
} else {
return XMLConstants.NULL_NS_URI;
}
}
從NameSpaceNevada對象的getNamespaceURI方法如下... 公共字符串getNamespaceURI(字符串前綴){ 如果(前綴== NULL){ 拋出新拋出:IllegalArgumentException( 「沒有前綴提供的!」); (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)){ }返回「http:// univNaSpResolver/book」;其他if(prefix.equals(「soapenv」)){「http://schemas.xmlsoap.org/soap/envelope/」;}; (prefix.equals(「tnew」)){0}返回「http:// PR_Library/TNeWebService」;其他{ }返回XMLConstants.NULL_NS_URI; } } – user3118724
我爲縮進道歉...我無法發佈答案,因爲我是新的論壇... – user3118724
要添加更多信息的問題,您使用_edit_函數 - 您可以隨時編輯您的自己的帖子,當你獲得聲望時,你也可以編輯其他人的帖子。 –