You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					132 lines
				
				5.8 KiB
			
		
		
			
		
	
	
					132 lines
				
				5.8 KiB
			| 
											3 years ago
										 | package com.ynxbd.wx.wxfactory.utils;
 | ||
|  | 
 | ||
|  | import com.alibaba.fastjson.JSON;
 | ||
|  | import com.ynxbd.wx.wxfactory.bean.MedicalNotify;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | import org.dom4j.*;
 | ||
|  | 
 | ||
|  | import java.util.HashMap;
 | ||
|  | import java.util.List;
 | ||
|  | import java.util.Map;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | public class XmlHelper {
 | ||
|  |     @FunctionalInterface
 | ||
|  |     public interface Params {
 | ||
|  |         void setMap(Map<String, Object> params);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 写入xml
 | ||
|  |      */
 | ||
|  |     public static String mapToXml(Params params) {
 | ||
|  |         Map<String, Object> map = new HashMap<>();
 | ||
|  |         params.setMap(map);
 | ||
|  |         return mapToXml(map);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 写入xml
 | ||
|  |      */
 | ||
|  |     public static String mapToXml(Map<String, Object> params) {
 | ||
|  |         //创建一个document
 | ||
|  |         Document document = DocumentHelper.createDocument();
 | ||
|  |         //增加根节点
 | ||
|  |         Element root = document.addElement("xml");
 | ||
|  |         Object value;
 | ||
|  |         for (Map.Entry<String, Object> entry : params.entrySet()) {
 | ||
|  |             value = entry.getValue();
 | ||
|  |             if (value == null) {
 | ||
|  |                 continue;
 | ||
|  |             }
 | ||
|  |             root.addElement(entry.getKey()).setText(value.toString());
 | ||
|  |         }
 | ||
|  |         return document.asXML();
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //    public static void main(String[] args) {
 | ||
|  | //        MchBaseResult mchBaseResult = new MchBaseResult();
 | ||
|  | //        mchBaseResult.setReturn_code("SUCCESS");
 | ||
|  | //        mchBaseResult.setReturn_msg("OK");
 | ||
|  | //        Map<String, Object> map = new HashMap<>();
 | ||
|  | //        map.put("return_code", "SUCCESS");
 | ||
|  | //        String s1 = mapToXml(map);
 | ||
|  | //        System.out.println(s1);
 | ||
|  | ////        String s = XMLConverUtil.convertToXML(map);
 | ||
|  | ////        System.out.println(s);
 | ||
|  | //    }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * xml转map(外层)
 | ||
|  |      *
 | ||
|  |      * @param xml     xml
 | ||
|  |      * @param isEmpty 是否转换空值
 | ||
|  |      * @return map
 | ||
|  |      */
 | ||
|  |     public static Map<String, Object> xmlOuterToMap(String xml, boolean isEmpty) {
 | ||
|  |         Map<String, Object> result = new HashMap<>();
 | ||
|  |         if (xml == null || "".equals(xml)) {
 | ||
|  |             return result;
 | ||
|  |         }
 | ||
|  |         Document document;
 | ||
|  |         try {
 | ||
|  |             document = DocumentHelper.parseText(xml);
 | ||
|  |             Element root = document.getRootElement(); // 获取根节点元素对象
 | ||
|  | 
 | ||
|  |             List<Element> listElement = root.elements(); // 所有一级子节点的list
 | ||
|  |             if (!listElement.isEmpty()) {
 | ||
|  |                 String name, content;
 | ||
|  |                 for (Element e : listElement) { // 遍历所有一级子节点
 | ||
|  |                     name = e.getName();
 | ||
|  |                     content = e.getTextTrim();
 | ||
|  |                     if (isEmpty) { //可为空
 | ||
|  |                         result.put(name, "".equals(content) ? null : content);
 | ||
|  |                     } else {
 | ||
|  |                         if (!"".equals(content)) {
 | ||
|  |                             result.put(name, content); // 沒有则将当前节点作为上级节点的属性对待
 | ||
|  |                         }
 | ||
|  |                     }
 | ||
|  |                 }
 | ||
|  |             }
 | ||
|  |         } catch (DocumentException e) {
 | ||
|  |             e.printStackTrace();
 | ||
|  |         }
 | ||
|  |         return result;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     public static void main(String[] args) {
 | ||
|  |         String xml = "<xml>\n" +
 | ||
|  |                 "<return_code><![CDATA[SUCCESS]]></return_code>\n" +
 | ||
|  |                 "<return_msg><![CDATA[SUCCESS]]></return_msg>\n" +
 | ||
|  |                 "<appid><![CDATA[wxd503671f502bd89d]]></appid>\n" +
 | ||
|  |                 "<mch_id><![CDATA[1288583001]]></mch_id>\n" +
 | ||
|  |                 "<openid><![CDATA[oeso-t5DIu2qpb0LJaKeJ06TRgzw]]></openid>\n" +
 | ||
|  |                 "<nonce_str><![CDATA[90690d6cf32c62ea]]></nonce_str>\n" +
 | ||
|  |                 "<med_trans_id><![CDATA[M22062955949633669]]></med_trans_id>\n" +
 | ||
|  |                 "<hosp_out_trade_no><![CDATA[WXa4820ed35506439d45d9d6d0635f]]></hosp_out_trade_no>\n" +
 | ||
|  |                 "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
 | ||
|  |                 "<time_end><![CDATA[20220629142511]]></time_end>\n" +
 | ||
|  |                 "<pay_type>3</pay_type>\n" +
 | ||
|  |                 "<total_fee>597</total_fee>\n" +
 | ||
|  |                 "<cash_fee>297</cash_fee>\n" +
 | ||
|  |                 "<insurance_fee>300</insurance_fee>\n" +
 | ||
|  |                 "<response_content><![CDATA[{\"directUrl\":\"http://dzpz.ynylbz.cn/test/hicashier/#payResult?encData=c785ab466a6d13d2a3f3f84c47bcc80fef109d5bc5212327aeac8687555eef618b9b77d789eebfcfa5021cd36a43a3f17d71daabe194f29565d530567c756072091e895012e6c3dda1042eac5b08e5fb98801010896c6bc3891314aa46b230a30c59aa91f8dbaf2ae3a5d1ec700154db8f7e4381a9b72e3dee8747e6811bd8fd8c90ff1411c42fa49ff871c1370ad791dc030b4c2185a515b2902c293cd2fb4661acdbc8457f5a7cc7dd801885d1a94162b72ee49f305a260894e10c7488f3325b5fcd21afa639392afb322c68101df10489b710394321e549fde466c19d4f25d70440bd496c2ba695b798ae4e90642b\",\"feeSumamt\":5.97,\"fundPay\":0.00,\"medSetlFlag\":\"SUCC\",\"ownPayAmt\":2.97,\"psnAcctPay\":3.00,\"setlSn\":\"SET530100202206291425070000006\"}]]></response_content>\n" +
 | ||
|  |                 "<medical_card_id><![CDATA[]]></medical_card_id>\n" +
 | ||
|  |                 "<serial_no><![CDATA[WXa4820ed35506439d45d9d6d0635f]]></serial_no>\n" +
 | ||
|  |                 "<attach><![CDATA[recipe=1]]></attach>\n" +
 | ||
|  |                 "<cash_order_id><![CDATA[C22062955949633669]]></cash_order_id>\n" +
 | ||
|  |                 "<insurance_order_id><![CDATA[I22062955949633669]]></insurance_order_id>\n" +
 | ||
|  |                 "<sign><![CDATA[1FA8E91777A02ADDDCE2AB02D96B20A3]]></sign>\n" +
 | ||
|  |                 "<insurance_self_fee>300</insurance_self_fee>\n" +
 | ||
|  |                 "<insurance_fund_fee>0</insurance_fund_fee>\n" +
 | ||
|  |                 "<insurance_other_fee>0</insurance_other_fee>\n" +
 | ||
|  |                 "</xml>";
 | ||
|  | 
 | ||
|  |         Map<String, Object> stringObjectMap = xmlOuterToMap(xml, false);
 | ||
|  |         MedicalNotify medicalNotify = JSON.parseObject(JSON.toJSONString(stringObjectMap), MedicalNotify.class);
 | ||
|  |         System.out.println(medicalNotify);
 | ||
|  |     }
 | ||
|  | }
 |