XML格式:
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>
解析XML后,通过HashMap<String,String>保存。
导入dom4j-1.6.1.jar包
代码如下:
public static HashMap<String,String> parseXML(HttpServletRequest request) throws Exception { HashMap<String,String> map = new HashMap<String,String>(); // 通过IO获得Document SAXReader reader = new SAXReader(); Document doc = reader.read(request.getInputStream()); // 得到xml的根节点 Element root = doc.getRootElement(); recursiveParseXML(root,map); return map; } public static void recursiveParseXML(Element root,HashMap<String,String> map) { // 得到根节点的子节点列表 List<Element> elementList = root.elements(); if (elementList.size() == 0) { map.put(root.getName(),root.getTextTrim()); } else { for (Element e : elementList) { recursiveParseXML(e,map); } } }原文链接:https://www.f2er.com/xml/295258.html