Dom4j 解析soap格式的xml數據


GitHub

目前只實現了3層結構的xml數據解析。
主要對以下格式的數據進行解析處理,實際格式可以根據開發實際需求進行修改關鍵字的監聽。
修改監聽對象在 getFirstElement()方法中修改值即可。

實現以下數據格式的解析

  1. 標準格式
  2. 嵌套數組
  3. 嵌套對象
  4. 數組嵌套對象
  5. 對象嵌套數組

實現類

import com.alibaba.fastjson.JSONObject;
import org.dom4j.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SoapXmlFormatUtil {

public class SoapXmlFormatUtil {

    public static JSONObject parse(String soapXml){
        JSONObject object = new JSONObject();
        try {
            JSONObject firstElement = getFirstElement(soapXml);
            JSONObject id0 = firstElement.getJSONObject("id0");
            for (String s : id0.keySet()) {
                Object s1 = id0.get(s);
                if (s1 instanceof List){
                    List list = new ArrayList();
                    List<String> stringList = (List<String>) s1;
                    for (String o : stringList) {
                        if (o.contains("#")){
                            list.add(traverseNode(firstElement, o));
                        }else {
                            list.add(o);
                        }
                    }
                    object.put(s, list);
                }else {
                    String key = s1.toString();
                    if (key.contains("#")){
                        object.put(s, traverseNode(firstElement, key));
                    }else {
                        object.put(s, key);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    private static JSONObject traverseNode(JSONObject rootElement, String key) throws Exception{
        JSONObject jsonObject = rootElement.getJSONObject(key.replaceAll("#", ""));
        for (String s2 : jsonObject.keySet()) {
            Object o1 = jsonObject.get(s2);
            if (o1 instanceof List){
                List<String> stringList1 = (List<String>) o1;
                List list1 = new ArrayList();
                for (String s3 : stringList1) {
                    if (s3.contains("#")){
                        JSONObject jsonObject1 = rootElement.getJSONObject(s3.replaceAll("#", ""));
                        list1.add(jsonObject1);
                    }else {
                        list1.add(s3);
                    }
                }
                jsonObject.put(s2, list1);
            }else {
                String key3 = o1.toString();
                if (key3.contains("#")){
                    JSONObject jsonObject1 = rootElement.getJSONObject(key3.replaceAll("#", ""));
                    jsonObject.put(s2, jsonObject1);
                }else {
                    jsonObject.put(s2, key3);
                }
            }
        }
        return jsonObject;
    }

    private static JSONObject getFirstElement(String soapXml) throws Exception{
        JSONObject jsonObject = new JSONObject();
        try {
            Document document = DocumentHelper.parseText(soapXml);
            Element rootElement = document.getRootElement();
            Iterator iterator = rootElement.elementIterator();
            while (iterator.hasNext()){
                Element next = (Element) iterator.next();
                Iterator elementIterator = next.elementIterator();
                while (elementIterator.hasNext()){
                    Element next1 = (Element) elementIterator.next();
                    String name = next1.getName();
                    if ("multiRef".equals(name)){
                        String id = next1.attribute("id").getValue();
                        JSONObject multiRefElement = getMultiRefElement(next1);
                        jsonObject.put(id, multiRefElement);
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    private static JSONObject getMultiRefElement(Element element) throws Exception{
        JSONObject jsonObject = new JSONObject();
        Iterator iterator1 = element.elementIterator();
        while (iterator1.hasNext()){
            Element next = (Element) iterator1.next();
            String text1 = next.getText();
            String name1 = next.getName();
            Attribute attribute = next.attribute("type");
            Attribute attributeHref = next.attribute("href");
            if (attribute != null){
                String attributeValue = attribute.getValue();
                if (attributeValue.toLowerCase().contains("array")){
                    List list = new ArrayList();
                    Iterator iterator2 = next.elementIterator();
                    while (iterator2.hasNext()){
                        Element next2 = (Element) iterator2.next();
                        Attribute attribute1 = next2.attribute("href");
                        if (attribute1 != null){
                            list.add(attribute1.getValue());
                        }else {
                            list.add(next2.getText());
                        }
                    }
                    jsonObject.put(name1, list);
                }else if (attributeValue.toLowerCase().contains("string")){
                    jsonObject.put(name1, text1);
                }
            }else if (attributeHref != null){
                String attributeValueHref = attributeHref.getValue();
                jsonObject.put(name1, attributeValueHref);
            }
        }
        return jsonObject;
    }

}

實現結果

import com.alibaba.fastjson.JSONObject;

public class Test3 {

    public static void main(String[] args) {
        String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<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\">\n" +
                " <soapenv:Body>\n" +
                "  <ns1:test soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://impl.prbt.cmcc.com\">\n" +
                "   <event href=\"#id0\"/>\n" +
                "  </ns1:test>\n" +
                "  <multiRef id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 xsi:type=\"xsd:string\">6</id5>\n" +
                "  </multiRef>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String message2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<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\">\n" +
                " <soapenv:Body>\n" +
                "  <ns1:test soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://impl.prbt.cmcc.com\">\n" +
                "   <event href=\"#id0\"/>\n" +
                "  </ns1:test>\n" +
                "  <multiRef id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 xsi:type=\"soapenc:Array\" soapenc:arrayType=\"xsd:string[1]\">\n" +
                "       <item>id5</item>\n" +
                "   </id5>\n" +
                "  </multiRef>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String message3 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<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\">\n" +
                " <soapenv:Body>\n" +
                "  <ns1:test soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://impl.prbt.cmcc.com\">\n" +
                "   <event href=\"#id0\"/>\n" +
                "  </ns1:test>\n" +
                "  <multiRef id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 href=\"#id1\"/>\n" +
                "  </multiRef>\n" +
                "  <multiRef id=\"id1\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 xsi:type=\"xsd:string\">6</id5>\n" +
                "  </multiRef>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String message4 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<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\">\n" +
                " <soapenv:Body>\n" +
                "  <ns1:test soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://impl.prbt.cmcc.com\">\n" +
                "   <event href=\"#id0\"/>\n" +
                "  </ns1:test>\n" +
                "  <multiRef id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 xsi:type=\"soapenc:Array\" soapenc:arrayType=\"xsd:string[1]\">\n" +
                "       <item href=\"#id1\"/>\n" +
                "   </id5>\n" +
                "  </multiRef>\n" +
                "  <multiRef id=\"id1\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 xsi:type=\"xsd:string\">6</id5>\n" +
                "  </multiRef>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String message5 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<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\">\n" +
                " <soapenv:Body>\n" +
                "  <ns1:test soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://impl.prbt.cmcc.com\">\n" +
                "   <event href=\"#id0\"/>\n" +
                "  </ns1:test>\n" +
                "  <multiRef id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5 href=\"#id1\"/>\n" +
                "  </multiRef>\n" +
                "  <multiRef id=\"id1\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns2:VRBTIssueProductNotifyEvt\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns2=\"http://schemas.prbt.cmcc.com\">\n" +
                "   <id0 xsi:type=\"xsd:string\">1</id0>\n" +
                "   <id1 xsi:type=\"xsd:string\">2</id1>\n" +
                "   <id2 xsi:type=\"xsd:string\">3</id2>\n" +
                "   <id3 xsi:type=\"xsd:string\">4</id3>\n" +
                "   <id4 xsi:type=\"xsd:string\">5</id4>\n" +
                "   <id5  xsi:type=\"soapenc:Array\" soapenc:arrayType=\"xsd:string[1]\">\n" +
                "       <item>id5</item>\n" +
                "   </id5>\n" +
                "  </multiRef>\n" +
                " </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        JSONObject parse = SoapXmlFormatUtil.parse(message);
        System.out.println("===========parse============");
        System.out.println(parse);
        JSONObject parse2 = SoapXmlFormatUtil.parse(message2);
        System.out.println("===========parse2============");
        System.out.println(parse2);
        JSONObject parse3 = SoapXmlFormatUtil.parse(message3);
        System.out.println("===========parse3============");
        System.out.println(parse3);
        JSONObject parse4 = SoapXmlFormatUtil.parse(message4);
        System.out.println("===========parse4============");
        System.out.println(parse4);
        JSONObject parse5 = SoapXmlFormatUtil.parse(message5);
        System.out.println("===========parse5============");
        System.out.println(parse5);
    }
}
===========parse============
{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":"6"}
===========parse2============
{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":["id5"]}
===========parse3============
{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":"6"}}
Disconnected from the target VM, address: '127.0.0.1:63155', transport: 'socket'
===========parse4============
{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":[{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":"6"}]}
===========parse5============
{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":{"id0":"1","id2":"3","id1":"2","id4":"5","id3":"4","id5":["id5"]}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章