解析微信公众号回调xml
时间:2020-06-19 11:48:20
收藏:0
阅读:168
引入依赖
<!-- 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;
}
希望我的分享对大家有所帮助。更多学习技巧也可参阅:福网!
评论(0)