Java解析.net asmx Soap返回包

Java 110次浏览 本站

背景
用.net asmx写webservice时,返回结果是xml格式,向下面这样:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <ProcessResponse xmlns="http://tempuri.org/">
      <ProcessResult>{"IsSuccess":true,"Body":{"msg":"成功","code":"1","sub_msg":"","sub_code":"","biz_data":{"message":"00000000000000034738","statusCode":200}}}</ProcessResult>
    </ProcessResponse>
  </soap:Body>
</soap:Envelope>


用.net 反序列化或者.net解析xml方式来解析这个结果都很方便。但是如果调用方使用的Java开发,则需要用Java来解析这个结果了。

解决办法
使用dom4j包来解析xml,解析上面的报文,也很简单,直接上代码:
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class Main {
 
    public static void main(final String[] args) {
        try {
 
            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><ProcessResponse xmlns=\"http://tempuri.org/\"><ProcessResult>{\"IsSuccess\":true,\"Body\":{\"msg\":\"成功\",\"code\":\"1\",\"sub_msg\":\"\",\"sub_code\":\"\",\"biz_data\":{\"message\":\"00000000000000034738\",\"statusCode\":200}}}</ProcessResult></ProcessResponse></soap:Body></soap:Envelope>";
            Document document = null;
            SAXReader saxReader = new SAXReader();
            InputStream is = new ByteArrayInputStream(xml.getBytes());
            document = saxReader.read(is);
            Element result = document.getRootElement().element("Body").element("ProcessResponse").element("ProcessResult");
            String json = result.getText();
            System.out.println(json);
 
        } catch (Exception e) {
            System.out.print("异常:" + e.getMessage());
            e.printStackTrace();
        }
    }
}
如果返回节点名称不一样,可以遍历整个xml,可以参考方法:
public static void parseXmlSax(String filename) throws Exception {
        Document document = null;
        SAXReader saxReader = new SAXReader();
        document = saxReader.read(new File(filename));
        Element root = document.getRootElement();
        System.out.println("根节点为:" + root.getName());
        Element elm = root.element("Body");
        List<?> bodyChild = elm.elements();
        for (Iterator<?> it = bodyChild.iterator(); it.hasNext();) {
            Element elm1 = (Element) it.next();
            System.out.println(elm1.getName());
            List<?> responseChild = elm1.elements();
            for (Iterator<?> it1 = responseChild.iterator(); it1.hasNext();) {
                Element elm2 = (Element) it1.next();
                System.out.println(elm2.getName() + " ===== " + elm2.getText());
            }
 
        }
    }
逐层遍历即可。

附:dom4j包下载地址:https://dom4j.github.io/

发表评论

电子邮件地址不会被公开。 必填项已用*标注