08-XMLUtil XMLUtil - XML文件 生成&解析 依赖 12345<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.3</version></dependency> 工具类 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.dom4j.io.SAXWriter;import java.io.ByteArrayInputStream;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;public class XmlUtil { /** * 生成 XML * @return 字符串 */ public static String createXML(Map<String, String> map) { //StringBuffer buffer=new StringBuffer(); //buffer.append("<xml>"); //for(String k:map.keySet()){ // buffer.append("<"+k+">"+map.get(k)+"</"+k+">"); //} //buffer.append("</xml>"); //return buffer.toString(); Document document = DocumentHelper.createDocument(); Element root = document.addElement("xml"); Set<String> keys = map.keySet(); for (String k : keys) { Element child = root.addElement(k); //child.setText("<![CDATA["+map.get(k)+"]]"); child.setText(map.get(k)); } return document.asXML(); } /** * 解析 * @return Map<String, String> */ public static Map<String, String> parseXml(String xml) { SAXReader reader = new SAXReader(); try { Document document = reader.read(new ByteArrayInputStream(xml.getBytes())); //获取根节点 Element root = document.getRootElement(); List<Element> list = root.elements(); Map<String, String> map = new HashMap<>(); for (Element e : list) { map.put(e.getName(), e.getTextTrim()); } return map; } catch (DocumentException e) { e.printStackTrace(); } return null; }} 21_代码片段 > 01_工具类 #工具类 08-XMLUtil https://janycode.github.io/2016/05/03/21_代码片段/01_工具类/08-XMLUtil/ 作者 Jerry(姜源) 发布于 2016年5月3日 许可协议 🔗 复制链接 📢 分享到微博 🐦 分享到 Twitter 📘 分享到 Facebook 💬 分享到微信 微信扫一扫分享 × 打开微信扫一扫,点击右上角分享 07-QrCodeUtil 上一篇 13-RedisUtils 下一篇 Please enable JavaScript to view the comments