Dom4j解析

  

 dom4j-1.6.1.jar,

这个包提供了xml解析相关的方法。

这里做一个记录,微信公众号里需要对HttpServletRequest做解析,实际上也可以用dom4j提供的方法进行解析转换。

这里直接上代码:

 1 /**
 2      * xml转换为map
 3      * 
 4      * @param request
 5      * @return
 6      * @throws IOException
 7      * @throws DocumentException
 8      */
 9     public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
10         Map<String, String> map = new HashMap<String, String>();
11         //生成解析器对象,使用的dom4j-1.6.1版本
12         SAXReader reader = new SAXReader();
13         //根据HttpServletRequest获得输入流
14         InputStream ins = request.getInputStream();
15         //通过SAXReader对象把输入流转换成Document对象
16         Document doc = reader.read(ins);
17 
18         Element root = doc.getRootElement();
19 
20         List<Element> list = root.elements();
21         for (Element e : list) {
22             map.put(e.getName(), e.getText());
23 
24         }
25         String json = UtilJackson.mapToJsonstr(map);
26         System.out.println("json:" + json);
27         ins.close();
28 
29         return map;
30     }
原文地址:https://www.cnblogs.com/Sunnor/p/6531627.html