2013-04-11 61 views
3

我想獲得這個soap故障的「細節」內的值,但我還沒有找到任何方式來這樣做。如何解析soapfault的細節?

來自服務器的響應:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
    <SOAP-ENV:Fault> 
     <faultcode>SOAP-ENV:Client</faultcode> 
     <faultstring>Many Errors</faultstring> 
     <detail> 
     <error_id>2</error_id> 
     <errors> 
      <error> 
      <error_id>1</error_id> 
      <error_description>Unknown Error</error_description> 
      </error> 
      <error> 
      <error_id>5</error_id> 
      <error_description>Not Authorized</error_description> 
      </error> 
      <error> 
      <error_id>9</error_id> 
      <error_description>Password should be at least 6 characters including one letter and one number</error_description> 
      </error> 
     </errors> 
     </detail> 
    </SOAP-ENV:Fault> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我需要與他們各自的error_description小號相處的error_id秒。到目前爲止,我只設法獲得通過KSOAP的detail用以下方式:

if (envelope.bodyIn instanceof SoapObject) { 
     return envelope.bodyIn.toString(); 
    } else if (envelope.bodyIn instanceof SoapFault) { 
     SoapFault e = (SoapFault) envelope.bodyIn; 
     Node details = ((SoapFault) envelope.bodyIn).detail; 

    } 

,但我沒有設法得到一個值,我需要當我嘗試「導航」,通過它。

任何幫助,非常感謝。我發現很少有非處理肥皂故障與ksoap2在線...

回答

0

畢竟它找出來。下面是做到這一點的方式:

 Node details = ((SoapFault) envelope.bodyIn).detail; 
     Element detEle = details.getElement(NAMESPACE, "detail"); 

     List<Error> errorList = new ArrayList<NewConnector.Error>(); 
     Element idEle = detEle.getElement(NAMESPACE, "error_id"); 
     str.append("id: " + idEle.getText(0)); 
     str.append("\n"); 
     Integer id = Integer.valueOf(idEle.getText(0)); 
     if (id == 2) { 
      // many errors 
      Element errors = detEle.getElement(NAMESPACE, "errors"); 
      int errorChildCount = errors.getChildCount(); 

      for (int i = 0; i < errorChildCount; i++) { 
       Object innerError = errors.getChild(i); 

       if (innerError instanceof Element) { 

        Element error_id = ((Element) innerError).getElement(
          NAMESPACE, "error_id"); 

        Element error_descrion = ((Element) innerError) 
          .getElement(NAMESPACE, "error_description"); 
        Error singleError = new Error(Integer.valueOf(error_id 
          .getText(0)), error_descrion.getText(0)); 

        errorList.add(singleError); 
        str.append(singleError.toString() + "\n"); 
       } 

      } 
      str.append("Found " + errorList.size() + " errors.\n"); 
      str.append("errorscount:" + errors.getChildCount()); 

代碼顯然需要改進,但它只是一個如何讓每個值的展示。乾杯

1

下面的代碼處理好

Iterator it = soapFaultClientException.getSoapFault().getFaultDetail().getDetailEntries(); 
while (it.hasNext()) 
{ 
Source errSource = it.next().getSource(); 
@SuppressWarnings("unchecked") 
JAXBElement errJaxb = (JAXBElement) springWebServiceTemplate.getUnmarshaller().unmarshal(errSource); 
ServerCustomizedError err = errJaxb.getValue(); 
.... 
} 
0

我用下面的方法:

/** 
* Method to retrieve the errorMessage from the given SoapFault. 
* @param soapFault 
* @return String representing the errorMessage found in the given SoapFault. 
*/ 
private static String getSoapErrorMessage (SoapFault soapFault) { 
    String errorMessage; 
    try { 
     Node detailNode = soapFault.detail; 
     Element faultDetailElement = (Element)detailNode.getElement(0).getChild(1); 
     Element errorMessageElement = (Element)faultDetailElement.getChild(0); 
     errorMessage = errorMessageElement.getText(0); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     errorMessage = "Could not determine soap error."; 
    } 
    return errorMessage; 
}