2016-08-05 11 views
0

コードとテキストを下記のsoap faultとは別に抽出したい。私が使用しているコード(xml以下に記載)は、コードとテキストを一緒に印刷しています。それは私がそれをXMLを解析し、オフフィールドを抽出するためにパーサを使用している、障害XMLであるため、SoapFaultException - コードとテキストを抽出する

<env:Fault xmlns:env = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:fault = "http://schemas.xmlsoap.org/soap/envelope/"> 
    <faultcode>fault:Client</faultcode> 
    <faultstring>An error occurred. Please check the detail section.</faultstring> 
    <detail> 
     <e:serviceFault xmlns:e = "http://xml.comcast.com/types"> 
      <e:messages> 
       <e:message> 
        <e:code>ERRORCODE-82828</e:code> 
        <e:text>Error Message.</e:text> 
       </e:message> 
      </e:messages> 
     </e:serviceFault> 
    </detail> 
</env:Fault> 

コードここ

public void printSoapFaultClientException(SoapFaultClientException e) { 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = null; 
    transformer = transformerFactory.newTransformer(); 

    DOMResult result = new DOMResult(); 

     transformer.transform(e.getSoapFault().getSource(), result); 
     NodeList nl = ((Document)result.getNode()).getElementsByTagName("detail"); 

    System.out.println(" text content " + ((Element)nl.item(0)).getTextContent()); 

} 

答えて

1

は、それを行うための一例です。また、SOAPFaultClientException APIを使用すると、必要に応じて障害理由を直接抽出できます(http://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/soap/client/SoapFaultClientException.html

File fXmlFile = new File("C:\\DevelopmentTools\\3.CODE\\SOAP.txt"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 
doc.getDocumentElement().normalize(); 

XPath xpath = XPathFactory.newInstance().newXPath(); 
String responseStatus = xpath.evaluate("//*[local-name()='code']/text()", doc); 
String responseText = xpath.evaluate("//*[local-name()='text']/text()", doc); 
System.out.println("---> " + responseStatus); 
System.out.println("---> " + responseText); 
+0

ありがとう@Ramachandran。 Spring APIはfaultCodeとfaultstringを返すだけで、内部コードとテキストは返されません。 –

+0

xpathが大いに役立ちました。ありがとうございました! –

+0

私はcatchブロック(SoapFaultClientException e)をキャッチしています。 内部テキストを抽出するにはどうすればよいですか? –