引入依赖
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
解析xml
/**
* 解析微信公众号回调xml
* @param request 请求
* @return
*/
public static Map<String,String> parseRequest(HttpServletRequest request){
Map<String,String> map=new HashMap<String,String>();
SAXReader reader=new SAXReader();
Document document= null;
try {
//读取输入流获取文档对象
document = reader.read(request.getInputStream());
//根据文档对象获取根节点
Element rootElement=document.getRootElement();
//获取根所有子节点
List<Element> elements = rootElement.elements();
for (Element e:elements) {
map.put(e.getName(),e.getStringValue());
}
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
return map;
}
来源:oschina
链接:https://my.oschina.net/u/3535099/blog/4316378