Я зацикливаюсь на этот ответ XML-ответа на мыло от SoapMessage. Я могу разобрать узлы и найти узел, который я хочу, но как вернуть значение узла обратно вызывающему методу. Поскольку я снова вызываю тот же метод, чтобы перейти к значению. Вот мой код.
Response XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<soapenv:Fault>
<soapenv:Code>
<soapenv:Value>soapenv:Receiver</soapenv:Value>
</soapenv:Code>
<soapenv:Reason>
<soapenv:Text xml:lang="en">Failed</soapenv:Text>
</soapenv:Reason>
<Detail xmlns="http://www.w3.org/2003/05/soap-envelope">
<axis2:documentFault xmlns:axis2="http://XXXXXXXXXXXXXXX" xmlns:ns2="http://XXXXXXXXXXXXXXX.xsd" xmlns:ns3="http://XXXXXXXXXXXXXXX.xsd">
<axis2:SystemInfo>
<axis2:ServiceContext>
<axis2:consumerPartnerId>XXX</axis2:consumerPartnerId>
</axis2:ServiceContext>
<axis2:ReturnInfo>
<axis2:returnSeverity>Error</axis2:returnSeverity>
<axis2:commonReturnMessage>Exception</axis2:commonReturnMessage>
<axis2:ReturnInfo>
<axis2:ReturnCode>008</axis2:ReturnCode>
<axis2:ReturnMessage>This is the response error description</axis2:ReturnMessage>
</axis2:ReturnInfo>
</axis2:ReturnInfo>
</axis2:SystemInfo>
</axis2:documentFault>
</Detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
Element dom = responseBody;
// Calling method
String soapResult = getChildren(dom);
// rest of the code
private String getChildren(Node n){
NodeList innerResultList = n.getChildNodes();
String value="";
for (int l = 0; l < innerResultList.getLength(); l++) {
Node currentNode = innerResultList.item(l);
System.out.println(currentNode.getNodeName());
String name = currentNode.getNodeName();
if(name.contains("Status")) {
value = currentNode.getTextContent();
System.out.println(value);
//result.put("result", value);
}else if(name.contains("ReturnMessage")) {
value = currentNode.getTextContent();
System.out.println(value);
//result.put("result", value);
}
getChildren(currentNode);
}
return value;
}
Я бы очень рекомендовал использовать XPath. В этом случае вы можете получить значение <axis2:ReturnMessage>
с двумя следующими строками:
XPath xPath = XPathFactory.newInstance().newXPath();
String returnMessage = xPath.compile("//Detail//ReturnMessage").evaluate(dom));
В //Detail//ReturnMessage
конкретном случае я ReturnMessage
первый ReturnMessage
предполагая, что у него есть элемент предка Detail
. Вы можете изменить эту логику выборки в соответствии с вашими потребностями.
EDIT: здесь полный источник по запросу OP:
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document dom = builder.parse(new FileInputStream("c://tmp//tmp.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println(xPath.compile("//Detail//ReturnMessage").evaluate(dom));
}
}
private Node getChildren(Node n)
методprivate Node getChildren(Node n)
? В этом случае с какой проблемой вы сталкиваетесь?