遍历SoapMessage主体中的所有元素并返回我想要的节点

时间:2015-03-21 19:13:43

标签: java soap recursion

我正在通过SoapMessage的这个soap fault XML响应进行循环。我能够通过节点解析并找到我想要的节点,但是如何将Node值返回给调用方法。因为我再次调用相同的方法来获取值。这是我的代码。

   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;
}

1 个答案:

答案 0 :(得分:2)

我强烈建议您使用XPath。在这种情况下,您可以使用以下两行获取<axis2:ReturnMessage>值:

XPath xPath =  XPathFactory.newInstance().newXPath();
String returnMessage = xPath.compile("//Detail//ReturnMessage").evaluate(dom));

//Detail//ReturnMessage特定情况下,我假设它有一个祖先元素ReturnMessage,我会获取第一个Detail。您可以根据需要修改此提取逻辑。

编辑:根据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));
    }    
}