提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import org.dom4j.Document; |
|
4 |
import org.dom4j.DocumentHelper; |
|
5 |
import org.dom4j.Element; |
|
6 |
import org.jdom.JDOMException; |
|
7 |
import org.jdom.input.SAXBuilder; |
|
8 |
import org.xml.sax.InputSource; |
|
9 |
|
|
10 |
import java.io.IOException; |
|
11 |
import java.io.StringReader; |
|
12 |
import java.util.*; |
|
13 |
|
|
14 |
public class HttpXmlUtils { |
|
15 |
|
|
16 |
@SuppressWarnings("rawtypes") |
|
17 |
public static String transferXml(SortedMap<Object, Object> parameters) { |
|
18 |
|
|
19 |
Document document = DocumentHelper.createDocument(); |
|
20 |
Element root = document.addElement("xml"); |
|
21 |
|
|
22 |
Set es = parameters.entrySet(); |
|
23 |
Iterator it = es.iterator(); |
|
24 |
while (it.hasNext()) { |
|
25 |
Map.Entry entry = (Map.Entry) it.next(); |
|
26 |
String k = (String) entry.getKey(); |
|
27 |
String v = (String) entry.getValue(); |
|
28 |
|
|
29 |
Element ToUserName = root.addElement(k); |
|
30 |
ToUserName.addText(v); |
|
31 |
} |
|
32 |
|
|
33 |
String queryString = document.asXML();// 转为String |
|
34 |
|
|
35 |
queryString = queryString.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").trim(); |
|
36 |
|
|
37 |
// System.out.println("企业端付款参数:"+queryString); |
|
38 |
return queryString; |
|
39 |
} |
|
40 |
|
|
41 |
@SuppressWarnings("unchecked") |
|
42 |
public static Map<String, String> parseRefundXml(String refundXml) throws JDOMException, IOException { |
|
43 |
|
|
44 |
StringReader read = new StringReader(refundXml); |
|
45 |
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 |
|
46 |
InputSource source = new InputSource(read); |
|
47 |
// 创建一个新的SAXBuilder |
|
48 |
SAXBuilder sb = new SAXBuilder(); |
|
49 |
// 通过输入源构造一个Document |
|
50 |
Document doc; |
|
51 |
doc = (Document) sb.build(source); |
|
52 |
Element root = doc.getRootElement();// 指向根节点 |
|
53 |
List<Element> list = root.elements(); |
|
54 |
Map<String, String> refundOrderMap = new HashMap<String, String>(); |
|
55 |
if (list != null && list.size() > 0) { |
|
56 |
for (Element element : list) { |
|
57 |
refundOrderMap.put(element.getName(), element.getText()); |
|
58 |
} |
|
59 |
return refundOrderMap; |
|
60 |
} |
|
61 |
return null; |
|
62 |
} |
|
63 |
} |