解析soap Xml

前端之家收集整理的这篇文章主要介绍了解析soap Xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@Test
public void main() {
    String deptXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            +"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:gateway=\"http://www.agree.com.cn/GDBGateway\">"
            +"<soapenv:Header>"
            +"<gateway:HeadType soapenv:actor=\"http://schemas.xmlsoap.org/soap/actor/next\" soapenv:mustUnderstand=\"0\">"
            +"<gateway:versionNo>1</gateway:versionNo>"
            +"</gateway:HeadType>"
            +"</soapenv:Header>"
            +"<soapenv:Body>"
            +"<gateway:NoAS400>"
            +"<gateway:field name=\"ERR_CODE\">CH0000</gateway:field>"
            +"<gateway:field name=\"ERR_MSG\">查询成功</gateway:field>"
            +"<gateway:field name=\"TOTAL_PAGE_NUM\">1</gateway:field>"
            +"<gateway:field name=\"CURRENT_PAGE_NUM\">1</gateway:field>"
            +"<gateway:field name=\"CURRENT_PAGE_COUNT\">2</gateway:field>"

            +"<gateway:field name=\"TX_DATE\">20170822</gateway:field>"
            +"<gateway:field name=\"TX_TIME\">055606</gateway:field>"
            +"<gateway:field name=\"IN_STAT\">借记卡1</gateway:field>"

            +"<gateway:field name=\"TX_DATE\">20170823</gateway:field>"
            +"<gateway:field name=\"TX_TIME\">055616</gateway:field>"
            +"<gateway:field name=\"IN_STAT\">借记卡2</gateway:field>"
            +"</gateway:NoAS400>"
            +"</soapenv:Body>"
            +"</soapenv:Envelope>";
    org.jsoup.nodes.Document doc = Jsoup.parse(deptXML);
    org.jsoup.select.Elements fieldEle = doc.getElementsByTag("gateway:field");
    String code = fieldEle.select("[name=ERR_CODE]").get(0).text();//取状态
    System.out.println("状态码:"+code);
    if(code!=null&&"CH0000".equals(code)){
        //请求成功处理
        int count = Integer.parseInt(fieldEle.select("[name=CURRENT_PAGE_COUNT]").get(0).text());
        //写法1
        for(int i=0;i<count;i++){
            System.out.println("第"+(i+1)+"个");
            org.jsoup.nodes.Element dateEle = fieldEle.select("[name=TX_DATE]").get(i);
            System.out.println("TX_DATE:"+dateEle.text());
            org.jsoup.nodes.Element timeEle = dateEle.nextElementSibling();
            System.out.println("TX_TIME:"+timeEle.text());
            org.jsoup.nodes.Element statEle = timeEle.nextElementSibling();
            System.out.println("IN_STAT:"+statEle.text());
        }
       System.out.println("-------分割线---------");
        //写法2
        for(int i=0;i<count;i++){
            System.out.println("第"+(i+1)+"个");
            org.jsoup.nodes.Element dateEle = fieldEle.select("[name=TX_DATE]").get(i);
            System.out.println("TX_DATE:"+dateEle.text());
            org.jsoup.nodes.Element timeEle = fieldEle.select("[name=TX_TIME]").get(i);
            System.out.println("TX_TIME:"+timeEle.text());
            org.jsoup.nodes.Element statEle = fieldEle.select("[name=IN_STAT]").get(i);
            System.out.println("IN_STAT:"+statEle.text());
}

    }
}
原文链接:https://www.f2er.com/xml/293855.html

猜你在找的XML相关文章