2014-01-17 97 views
2

雖然我看到類似的帖子,我似乎無法解決這個問題(所以我手動解析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; 
    } 
} 
+0

從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

+0

我爲縮進道歉...我無法發佈答案,因爲我是新的論壇... – user3118724

+0

要添加更多信息的問題,您使用_edit_函數 - 您可以隨時編輯您的自己的帖子,當你獲得聲望時,你也可以編輯其他人的帖子。 –

回答

0

而不是評估所述表達載的節點,然後做複雜的DOM操作到提取文本值,您可以只將表達式評估爲字符串。

expr.evaluate(documentoXML); 

將評估表達和使用XPath規則將結果轉換爲字符串,在這種情況下會給你的第一個(其實是唯一的)tnew:OpcionCarga元素的文本內容。

但爲什麼它不工作的真正原因是,Java的DocumentBuilderFactory在默認情況下(在很多人眼中一個非常糟糕的設計決策,而是一個現在還不能在不破壞向後兼容性進行修正)非命名空間感知 - 你爲了創建一個命名空間感知的解析器調用.newDocumentBuilder()之前,你必須做

factory.setNamespaceAware(true); 

+0

謝謝你回答Ian ...我會努力的......同時我所做的就是「清理」xml鏈並留下沒有前綴......我肯定會嘗試...謝謝x再次 – user3118724

+0

@ user3118724即使文檔沒有實際使用任何名稱空間,仍然需要解析它,以確保XPath能夠正常工作。 XPath語言僅在名稱空間良好的XML上定義。 –