使用dom4j解析xml文件,并转出json格式问题
作者:zhao_854093140
这篇文章主要介绍了使用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" }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。