package com.hx.mp.util; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.xml.sax.InputSource; import java.io.IOException; import java.io.StringReader; import java.util.*; public class HttpXmlUtils { @SuppressWarnings("rawtypes") public static String transferXml(SortedMap parameters) { Document document = DocumentHelper.createDocument(); Element root = document.addElement("xml"); Set es = parameters.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); String v = (String) entry.getValue(); Element ToUserName = root.addElement(k); ToUserName.addText(v); } String queryString = document.asXML();// 转为String queryString = queryString.replace("", "").trim(); // System.out.println("企业端付款参数:"+queryString); return queryString; } @SuppressWarnings("unchecked") public static Map parseRefundXml(String refundXml) throws JDOMException, IOException { StringReader read = new StringReader(refundXml); // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 InputSource source = new InputSource(read); // 创建一个新的SAXBuilder SAXBuilder sb = new SAXBuilder(); // 通过输入源构造一个Document Document doc; doc = (Document) sb.build(source); Element root = doc.getRootElement();// 指向根节点 List list = root.elements(); Map refundOrderMap = new HashMap(); if (list != null && list.size() > 0) { for (Element element : list) { refundOrderMap.put(element.getName(), element.getText()); } return refundOrderMap; } return null; } }