1. 程式人生 > >將map自動轉化為xml報文

將map自動轉化為xml報文

public static byte[] callMapToXML(Map map) {
		logger.info("將Map轉成Xml, Map:" + map.toString());
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><bizdata>");
		mapToXMLTest2(map, sb);
		sb.append("</bizdata>");
		logger.info("將Map轉成Xml, Xml:" + sb.toString());
		try {
			return sb.toString().getBytes("UTF-8");
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	private static void mapToXMLTest2(Map map, StringBuffer sb) {
		Set set = map.keySet();
		for (Iterator it = set.iterator(); it.hasNext();) {
			String key = (String) it.next();
			Object value = map.get(key);
			if (null == value)
				value = "";
			if (value.getClass().getName().equals("java.util.ArrayList")) {
				ArrayList list = (ArrayList) map.get(key);
				sb.append("<" + key + ">");
				for (int i = 0; i < list.size(); i++) {
					HashMap hm = (HashMap) list.get(i);
					mapToXMLTest2(hm, sb);
				}
				sb.append("</" + key + ">");

			} else {
				if (value instanceof HashMap) {
					sb.append("<" + key + ">");
					mapToXMLTest2((HashMap) value, sb);
					sb.append("</" + key + ">");
				} else {
					sb.append("<" + key + ">" + value + "</" + key + ">");
				}

			}

		}
	}

待轉化的多層級map程式碼:

public static void main(String[] args) {
		Map<String, Object> dataMap = new LinkedHashMap<String, Object>(); //預設
		dataMap.put("rtnCode", "02");
		dataMap.put("rtnMsg", "查詢失敗");
		dataMap.put("idWltCloudDistrict", "專區id");
		
		Map<String, Object> bizMap = new LinkedHashMap<String, Object>();
		bizMap.put("serviceId", "serviceId001");
		bizMap.put("data", dataMap);//data節點是一個map
		
		Map<String, Object> objMap = new LinkedHashMap<String, Object>();
		objMap.put("idWltCloudDistrict", "專區id");
		objMap.put("districtName", "專區名稱");
		objMap.put("validateDateStart", "專區有效期開始");
		objMap.put("validateDateEnd", "專區有效期結束");
		objMap.put("status", "專區狀態(00-待發布;01-銷售中; 02-已下架;)");
		objMap.put("partnerId", "合作伙伴id");
		objMap.put("channelId", "渠道id");
		objMap.put("areaId", "商圈專區id");
		dataMap.putAll(objMap); //
		
		List<Map<String, Object>> prodKindList1 = new ArrayList<Map<String, Object>>();
		for(int i=1;i<=2;i++){
			// 第二層
			List<Map<String, Object>> prodKindList2 = new ArrayList<Map<String, Object>>();
			for(int j=1;j<=2;j++){
				Map<String, Object> prodKindObj2 = new LinkedHashMap<String, Object>();
				prodKindObj2.put("idWltCloudProdKind", "類目ID");
				prodKindObj2.put("prodKindName", "類目名稱");
				prodKindObj2.put("prodKindIdx", "類目序號");
				Map<String, Object> prodKind2 = new LinkedHashMap<String, Object>();
				prodKind2.put("prodKind", prodKindObj2);
				prodKindList2.add(prodKind2);
			}
			
			//第一層
			Map<String, Object> prodKindOjb1 = new LinkedHashMap<String, Object>();
			prodKindOjb1.put("idWltCloudProdKind", "類目ID");
			prodKindOjb1.put("prodKindName", "類目名稱");
			prodKindOjb1.put("prodKindIdx", "類目序號");
			prodKindOjb1.put("prodKindList", prodKindList2);
			Map<String, Object> prodKind1 = new LinkedHashMap<String, Object>();
			prodKind1.put("prodKind", prodKindOjb1);
			prodKindList1.add(prodKind1);
		}
		
		dataMap.put("prodKindList", prodKindList1);
		
		XmlCommonUtil.callMapToXML(bizMap);
	}


轉化後效果:

<?xml version="1.0" encoding="utf-8"?>
<bizdata>
    <serviceId>serviceId001</serviceId>
    <data>
        <rtnCode>02</rtnCode>
        <rtnMsg>查詢失敗</rtnMsg>
        <idWltCloudDistrict>專區id</idWltCloudDistrict>
        <districtName>專區名稱</districtName>
        <validateDateStart>專區有效期開始</validateDateStart>
        <validateDateEnd>專區有效期結束</validateDateEnd>
        <status>專區狀態(00-待發布;01-銷售中; 02-已下架;)</status>
        <partnerId>合作伙伴id</partnerId>
        <channelId>渠道id</channelId>
        <areaId>商圈專區id</areaId>
        <prodKindList>
            <prodKind>
                <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                <prodKindName>類目名稱</prodKindName>
                <prodKindIdx>類目序號</prodKindIdx>
                <prodKindList>
                    <prodKind>
                        <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                        <prodKindName>類目名稱</prodKindName>
                        <prodKindIdx>類目序號</prodKindIdx>
                    </prodKind>
                    <prodKind>
                        <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                        <prodKindName>類目名稱</prodKindName>
                        <prodKindIdx>類目序號</prodKindIdx>
                    </prodKind>
                </prodKindList>
            </prodKind>
            <prodKind>
                <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                <prodKindName>類目名稱</prodKindName>
                <prodKindIdx>類目序號</prodKindIdx>
                <prodKindList>
                    <prodKind>
                        <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                        <prodKindName>類目名稱</prodKindName>
                        <prodKindIdx>類目序號</prodKindIdx>
                    </prodKind>
                    <prodKind>
                        <idWltCloudProdKind>類目ID</idWltCloudProdKind>
                        <prodKindName>類目名稱</prodKindName>
                        <prodKindIdx>類目序號</prodKindIdx>
                    </prodKind>
                </prodKindList>
            </prodKind>
        </prodKindList>
    </data>
</bizdata>

樓主這麼辛苦,請掃一下樓主的支付寶紅包推薦碼吧,記得一定要消費掉哦。雙贏哦。

1、開啟支付寶首頁搜尋“8282987” 立即領紅包。

2、掃碼領紅包。

領支付寶紅包啦

標題。