使用dom4j解析xml文件,并转出json格式

使用dom4j解析xml文件,并转出json格式

我这边的需要就是讲xml格式的soap的响应报文解析出来并返回给前端
这个需求我本来是不会写的 然后找我师父帮我写的 ,我怕以后自己遇到同样的问题,所以在这里记录下来
首先要准备的依赖

<!--dom4j-->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
String xml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <soap:Body>\n" +
                "        <ns2:askResponse xmlns:ns2=\"http://www.eastrobot.cn/ws/AskService\" xmlns:ns3=\"http://www.eastrobot.cn/ws/RobotServiceEx\">\n" +
                "            <robotResponse>\n" +
                "                <commands>\n" +
                "                    <args>ff8080816bf47a1b016c8f39b153000b</args>\n" +
                "                    <args>ff8080816bf47a1b016c8f3a1c4d000e</args>\n" +
                "                    <name>prop.categoryId</name>\n" +
                "                    <state>0</state>\n" +
                "                </commands>\n" +
                "                <commands>\n" +
                "                    <args>ff8080816bf47a1b016c8f3a1c4d000e</args>\n" +
                "                    <name>prop.objectId</name>\n" +
                "                    <state>0</state>\n" +
                "                </commands>\n" +
                "                <commands>\n" +
                "                    <args>94654758cf484e249bc71b52d6291cba</args>\n" +
                "                    <name>prop.sessionId</name>\n" +
                "                    <state>0</state>\n" +
                "                </commands>\n" +
                "                <content>测试答案</content>\n" +
                "                <moduleId>core</moduleId>\n" +
                "                <nodeId>00156577104596500003005056aa3f45</nodeId>\n" +
                "                <relatedQuestions>测试问题</relatedQuestions>\n" +
                "                <similarity>1.0</similarity>\n" +
                "                <type>1</type>\n" +
                "            </robotResponse>\n" +
                "        </ns2:askResponse>\n" +
                "    </soap:Body>\n" +
                "</soap:Envelope>";

这是一个xml格式的soap的响应报文,现在的需求就是将这个报文解析出来,我这里使用的是dom4j解析

public static JsonObject xmlToJson(String xml) throws Exception{
        //创建jsonobject
        JsonObject object = new JsonObject();
        //创建JsonArray
        JsonArray array = new JsonArray();
        //将array添加到object中
        object.add("commands",array);
        //创建一个SAXReader阅读器
        SAXReader reader = new SAXReader();
        //根据字符串构建字节数组输入流
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        //用阅读器读取文档对象
        Document document = reader.read(is);
        //获取文档对象的根节点
        log.info("开始解析soap报文");
        Element root = document.getRootElement();
        //遍历子节点
        for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
            Element bodyElement = (Element) iterator.next();
            for (Iterator iterator1 = bodyElement.elementIterator(); iterator1.hasNext(); ) {
                Element askResponseElement = (Element) iterator1.next();
                for (Iterator iterator2 = askResponseElement.elementIterator(); iterator2.hasNext(); ) {
                    Element robotResponseElement = (Element) iterator2.next();
                    for (Iterator iterator3 = robotResponseElement.elementIterator(); iterator3.hasNext(); ) {
                        Element element = (Element) iterator3.next();
                        String name = element.getName();
                        if ("commands".equals(name)) {
                            JsonObject ele = new JsonObject();
                            array.add(ele);
                            JsonArray subArray = new JsonArray();
                            ele.add("args",subArray);
                            for (Iterator iterator4 = element.elementIterator(); iterator4.hasNext(); ) {
                                Element subElement = (Element) iterator4.next();
                                String subName = subElement.getName();
                                if ("args".equalsIgnoreCase(subName)){
                                    subArray.add(subElement.getStringValue());
                                }else {
                                    ele.addProperty(subName, subElement.getStringValue());
                                }
                                //  System.out.println(subElement.getName() + ":" + subElement.getStringValue());
                            }
                        } else {
                            object.addProperty(name, element.getStringValue());
                            // System.out.println(name + ":" + element.getStringValue());
                        }
                    }
                }
            }
        }
        log.info("json格式的报文={}",object);
        return object;
    }

解析出来的就是这样的

{
  "commands": [
    {
      "args": [
        "ff8080816bf47a1b016c8f39b153000b",
        "ff8080816bf47a1b016c8f3a1c4d000e"
      ],
      "name": "prop.categoryId",
      "state": "0"
    },
    {
      "args": [
        "ff8080816bf47a1b016c8f3a1c4d000e"
      ],
      "name": "prop.objectId",
      "state": "0"
    },
    {
      "args": [
        "94654758cf484e249bc71b52d6291cba"
      ],
      "name": "prop.sessionId",
      "state": "0"
    }
  ],
  "content": "测试答案",
  "moduleId": "core",
  "nodeId": "00156577104596500003005056aa3f45",
  "relatedQuestions": "测试问题",
  "similarity": "1.0",
  "type": "1"
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值