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 = "\n" +
                "\n" +
                " \n" +
                "  \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   6\n" +
                "  \n" +
                " \n" +
                "";

        String message2 = "\n" +
                "\n" +
                " \n" +
                "  \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   \n" +
                "       id5\n" +
                "   \n" +
                "  \n" +
                " \n" +
                "";

        String message3 = "\n" +
                "\n" +
                " \n" +
                "  \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   6\n" +
                "  \n" +
                " \n" +
                "";

        String message4 = "\n" +
                "\n" +
                " \n" +
                "  \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   \n" +
                "       \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   6\n" +
                "  \n" +
                " \n" +
                "";

        String message5 = "\n" +
                "\n" +
                " \n" +
                "  \n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   \n" +
                "  \n" +
                "  \n" +
                "   1\n" +
                "   2\n" +
                "   3\n" +
                "   4\n" +
                "   5\n" +
                "   \n" +
                "       id5\n" +
                "   \n" +
                "  \n" +
                " \n" +
                "";

        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"]}}

你可能感兴趣的:(JAVA)