MapToXmlUtils工具

   /**
     * xml string convert to mao object
     * @param str
     * @throws Exception 
     */
    public static Map<String, Object> xmlFormatStringToMap(String str) throws Exception {
    
    
        if (!StringUtils.isEmpty(str)) {
    
    
            try {
    
    
                Element root = DocumentHelper.parseText(str).getRootElement();
                // 获取所有子节点
                List<Element> elements = root.elements();
                Map<String, Object> result = null;
                for (Element element : elements) {
    
    
                    // 第一次进入初始化
                    if (result == null){
    
    
                        result = new HashMap<>();
                    }
                    result.put(element.getName(), element.getText());
                }
                return result;
            } catch (DocumentException e) {
    
    
                LOG.error("Xml转换异常 >>> 被转换字符串 >>> " + str);
                throw new Exception("Xml转换异常");
            }
        } else {
    
    
            return new HashMap<>();
        }
    }

/**
     * Map convert Xml string
     * @param params
     * @author 
     */
    public static String mapToXmlFormatString(Map<String, Object> params) {
    
    
        // add xml head
        String head = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        return head + convert(params);
    }
/**
     * 转换
     * @author 
     * @param map
     */
    private static String convert(Map<String, Object> map) {
    
    
        // 值类型
        // 最后结果标签
        StringBuffer buffer = new StringBuffer();
        for (String key : map.keySet()) {
    
    
            // 拼接xml  开头标签
            buffer.append("<" + key + ">");
            // 判断value值是否为HashMap类型
            if (map.get(key) instanceof Map) {
    
    
                String element = convert((Map<String, Object>) map.get(key));
                buffer.append(element);
            } else {
    
    
               buffer.append(map.get(key));
            }
            // 收尾标签
            buffer.append("</" + key + ">");
        }
        return buffer.toString();
    }

Map==>xml测试结果

public static void main(String[] args) {
    
    
  HttpUtils http = new HttpUtils();
  Map<String,Object> Input = new HashMap<String,Object>();
  Map<String,Object> InHead = new HashMap<String,Object>();
  Map<String,Object> InBody = new HashMap<String,Object>(); 
  
  InHead.put("BusinessNumber", "BusinessNumber");
  InHead.put("TerminalNumber", "TerminalNumber");
  InHead.put("CooperationUnit", "CooperationUnit");
  InBody.put("PatientId", "PatientId");
  InBody.put("BillNo", "BillNo");
  InBody.put("BillMethod", "BillMethod");
  InBody.put("Price", "Price");
  InBody.put("PatientId","PatientId");
  
  Input.put("InHead", InHead);
  Input.put("InBody", InBody);
  Map<String,Object> xmlmap = new HashMap<String,Object>();
  xmlmap.put("Input", Input);
  System.out.println(http.mapToXmlFormatString(xmlmap));

结果:

<?xml version="1.0" encoding="utf-8"?><Input><InHead><BusinessNumber>BusinessNumber</BusinessNumber><CooperationUnit>CooperationUnit</CooperationUnit><TerminalNumber>TerminalNumber</TerminalNumber></InHead><InBody><BillNo>BillNo</BillNo><BillMethod>BillMethod</BillMethod><Price>Price</Price><PatientId>PatientId</PatientId></InBody></Input>

格式化后:

<?xml version="1.0" encoding="utf-8"?>
<Input>
    <InHead>
        <BusinessNumber>BusinessNumber</BusinessNumber>
        <CooperationUnit>CooperationUnit</CooperationUnit>
        <TerminalNumber>TerminalNumber</TerminalNumber>
    </InHead>
    <InBody>
        <BillNo>BillNo</BillNo>
        <BillMethod>BillMethod</BillMethod>
        <Price>Price</Price>
        <PatientId>PatientId</PatientId>
    </InBody>
</Input>

猜你喜欢

转载自blog.csdn.net/weixin_40550118/article/details/105479201