gpt4 book ai didi

java - SoapFaultException - 提取代码和文本

转载 作者:行者123 更新时间:2023-12-01 09:37:14 25 4
gpt4 key购买 nike

我想从下面列出的肥皂故障中单独提取代码和文本。我正在使用的代码(在 xml 下面列出)将代码和文本一起打印。

<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());

}

最佳答案

这是一个执行此操作的示例,因为它是一个错误 XML,所以我刚刚使用解析器来解析 XML 并从中提取字段。如果您需要的话,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);

关于java - SoapFaultException - 提取代码和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38780870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com