package com.answern.claimv2.service.utils; import com.alibaba.dubbo.common.utils.IOUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; public class XmlUtil { public Map<String,Object> map = new HashMap<>(); public Map parse(String soap) throws DocumentException { Document doc = DocumentHelper.parseText(soap);//报文转成doc对象 Element root = doc.getRootElement();//获取根元素,准备递归解析这个XML树 getCode(root); return map; } public void getCode(Element root) { if (root.elements() != null) { List<Element> list = root.elements();//如果当前跟节点有子节点,找到子节点 for (Element e : list) {//遍历每个节点 if (e.elements().size() > 0) { getCode(e);//当前节点不为空的话,递归遍历子节点; } if (e.elements().size() == 0) { map.put(e.getName(), e.getTextTrim()); }//如果为叶子节点,那么直接把名字和值放入map } } } public static void main(String[] args) throws Exception{ String[] strings = IOUtils.readLines(new File("C:\\Users\\Administrator\\Desktop\\返回报文.xml")); StringBuffer sb = new StringBuffer(); for (String s : strings){ sb.append(s); } //初始化报文,调用parse方法,获得结果map,然后按照需求取得字段,或者转化为其他格式 Map map = new XmlUtil().parse(sb.toString()); System.out.println(map.get("response_code")); } }
<PACKET> <responsehead> <response_code>1</response_code> <error_message>成功!</error_message> </responsehead> <responsebody></responsebody> </PACKET>