fwq
2023-10-26 0c97aac57ef013e764f9e919a42596de4cf8a629
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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<Object, Object> 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").trim();
 
        // System.out.println("企业端付款参数:"+queryString);
        return queryString;
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String, String> 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<Element> list = root.elements();
        Map<String, String> refundOrderMap = new HashMap<String, String>();
        if (list != null && list.size() > 0) {
            for (Element element : list) {
                refundOrderMap.put(element.getName(), element.getText());
            }
            return refundOrderMap;
        }
        return null;
    }
}