import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.Map;
public class XMLUtil { private static final Logger LOGGER = LoggerFactory.getLogger(XMLUtil.class); /** * 默认编码 */ private static final String DEFAULT_ENCODING = "UTF-8"; /** * 创建一个新的XML文档 */ public static Document createDocument() { return DocumentHelper.createDocument(); } /** * 获取xml 文档 * * @param inputXml * @return */ public static Document getDocument(InputStream inputXml) { Document document = null; SAXReader saxReader = null; try { saxReader = new SAXReader(); document = saxReader.read(inputXml); } catch (DocumentException e) { LOGGER.error(e.getMessage(), e); } return document; } /** * 添加元素 * * @param document * @param name * @return */ public static Element addElement(Document document, String name) { return document.addElement(name); } /** * 给元素中添加注释 * * @param obj * @param comment */ public static void addComment(Object obj, String comment) { if (obj instanceof Document) { ((Document) obj).addComment(comment); } else if (obj instanceof Element) { ((Element) obj).addComment(comment); } } /** * 给元素添加属性和属性值 * * @param name * @param value */ public static void setAttribute(Element element, String name, String value) { element.addAttribute(name, value); } /** * 写xml文件 * * @param outXml * @param document * @param encoding */ public static void writerXML(OutputStream outXml, Document document, String encoding) { OutputFormat format = OutputFormat.createPrettyPrint(); if (StringUtils.isBlank(encoding)) { encoding = DEFAULT_ENCODING; } format.setEncoding(encoding); XMLWriter out = null; try { out = new XMLWriter(outXml, format); out.write(document); out.flush(); } catch (UnsupportedEncodingException e) { LOGGER.error(e.getMessage(), e); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } } } public static Map<String, String> parserXml(String xml) { Map<String, String> responseMap = new HashMap<String, String>(); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(DEFAULT_ENCODING))); Element employees = document.getRootElement(); for (Iterator i = employees.elementIterator(); i.hasNext(); ) { Element employee = (Element) i.next(); Iterator j = employee.elementIterator(); if (j.hasNext()) { for (; j.hasNext(); ) { Element nlement = (Element) j.next(); responseMap.put(employee.getName(), employee.getText()); } } else { responseMap.put(employee.getName(), employee.getText()); } } } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return responseMap; } }