xml、map、json相互轉換
練習四
1、瞭解xml 格式以及解析方法,掌握xml與map互轉的方法
2、瞭解json格式以及解析方法,掌握json與map互轉的方法
3、新建測試方法,將以下xml格式報文轉換成json格式報文
以下一個練習將會包含到xml2map、map2json,所以間接形成了xml2json。
反向轉換也可以參考轉換時用到的工具類和方法
interBoss.xml檔案
點選檢視程式碼
<?xml version="1.0" encoding="UTF-8"?> <SvcRoot> <Item> <Name>IndictSeq</Name> <Value>20210726CSVC2700000003</Value> </Item> <Item> <Name>CallerNo</Name> <Value>15145673833</Value> </Item> <Item> <Name>CalledNo</Name> <Value>10086</Value> </Item> <Item> <Name>MSISDN</Name> <Value>15145673833</Value> </Item> <Item> <Name>SubsName</Name> <Value>地方換個電話</Value> </Item> <Item> <Name>SubsLevel</Name> <Value>04</Value> </Item> <Item> <Name>SubsBrand</Name> <Value>01</Value> </Item> <Item> <Name>SvcTypeId</Name> <Value>10010105020101</Value> </Item> <Item> <Name>UrgentId</Name> <Value>03</Value> </Item> <Item> <Name>HomeProv</Name> <Value>270</Value> </Item> <Item> <Name>SvcCity</Name> <Value>027-武漢</Value> </Item> <Item> <Name>TProv</Name> <Value>000</Value> </Item> <Item> <Name>AcceptTime</Name> <Value>2021-07-26 10:51:40</Value> </Item> <Item> <Name>AcceptStaff</Name> <Value>3406</Value> </Item> <Item> <Name>OriginTime</Name> <Value>2021-07-26 10:51:40</Value> </Item> <Item> <Name>DeadTime</Name> <Value>2021-07-27 10:51:40</Value> </Item> <Item> <Name>Title</Name> <Value>test</Value> </Item> <Item> <Name>ProcessType</Name> <Value>1</Value> </Item> <Item> <Name>ContactChannel</Name> <Value>01</Value> </Item> <Item> <Name>ServiceContent</Name> <Value>測試工單</Value> </Item> <Item> <Name>ContactId</Name> <Value>21072601000000004</Value> </Item> <Item> <Name>ServiceName</Name> <Value>94</Value> </Item> <Item> <Name>SRId</Name> <Value>2107260000000011</Value> </Item> <Item> <Name>MobileNewsNum</Name> <Value>001</Value> </Item> </SvcRoot>
實現過程Xml2Json.java
點選檢視程式碼
public class Xml2Json { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); try { InputStream is = new FileInputStream(new File("src/main/resources/test/interBOSS.xml")); SAXReader sax = new SAXReader(); //建立解析器 Document doc = sax.read(is); //read方法將配置讀到記憶體中,建立對應的Document[org.dom4j]物件樹 Element root = doc.getRootElement(); //獲取XML文件的根節點物件 //遍歷根節點 HashMap<String, Object> rootMap = new HashMap<>(); HashMap<String, Object> SvcContMap = new HashMap<>(); HashMap<String, Object> CDATAMap = new HashMap<>(); HashMap<String, Object> SvcRootMap = new HashMap<>(); List<Map<String, Object>> itemMapList = new ArrayList<>(); Map<String, Object> interBOSSMap = new HashMap<>(); List<Map<String, Object>> svcContList = new ArrayList<>(); for (Iterator<Element> rootIter = root.elementIterator(); rootIter.hasNext(); ) { Map<String, Object> svcContMap = new HashMap<>(); Element svcContElt = rootIter.next(); Document doc2 = DocumentHelper.parseText(svcContElt.getText()); Element root2 = doc2.getRootElement(); List<Map<String, Object>> svcRootList = new ArrayList<>(); for (Iterator<Element> svcContIter = root2.elementIterator(); svcContIter.hasNext(); ) { Map<String, Object> svcRootMap = new HashMap<>(); Element itemEle = svcContIter.next(); Map<String, Object> itemMap = new HashMap<>(); Map<String, Object> itemSubMap = new HashMap<>(); for (Iterator<Element> itemIter = itemEle.elementIterator(); itemIter.hasNext(); ) { Element svcRootElt = itemIter.next(); itemSubMap.put(svcRootElt.getName(), svcRootElt.getText()); } itemMap.put("Item", itemSubMap); svcRootList.add(itemMap); } if(svcRootList.size() == 1){ svcContMap.put("SvcRoot", svcRootList.get(0)); }else{ svcContMap.put("SvcRoot", svcRootList); } svcContList.add(svcContMap); } if(svcContList.size() == 1){ interBOSSMap.put("SvcCont", svcContList.get(0)); }else { interBOSSMap.put("SvcCont", svcContList); } rootMap.put("InterBOSS", interBOSSMap); System.out.println("map物件:" + rootMap.toString()); //map轉換為json。從而實現xml到json的轉換 String strMap2Json = JsonUtils.javaMap2Json(rootMap); System.out.println("Json字串:" + strMap2Json); } catch (Exception e) { e.printStackTrace(); } } }
執行實現結果:
Json字串:{"InterBOSS":{"SvcCont":{"SvcRoot":[{"Item":{"Name":"IndictSeq","Value":"20210726CSVC2700000003"}},{"Item":{"Name":"CallerNo","Value":"18271403993"}},{"Item":{"Name":"CalledNo","Value":"10086"}},{"Item":{"Name":"MSISDN","Value":"18271403993"}},{"Item":{"Name":"SubsName","Value":"*樹巨集"}},{"Item":{"Name":"SubsLevel","Value":"04"}},{"Item":{"Name":"SubsBrand","Value":"01"}},{"Item":{"Name":"SvcTypeId","Value":"10010105020101"}},{"Item":{"Name":"UrgentId","Value":"03"}},{"Item":{"Name":"HomeProv","Value":"270"}},{"Item":{"Name":"SvcCity","Value":"027-武漢"}},{"Item":{"Name":"TProv","Value":"000"}},{"Item":{"Name":"AcceptTime","Value":"2021-07-26 10:51:40"}},{"Item":{"Name":"AcceptStaff","Value":"3406"}},{"Item":{"Name":"OriginTime","Value":"2021-07-26 10:51:40"}},{"Item":{"Name":"DeadTime","Value":"2021-07-27 10:51:40"}},{"Item":{"Name":"Title","Value":"test"}},{"Item":{"Name":"ProcessType","Value":"1"}},{"Item":{"Name":"ContactChannel","Value":"01"}},{"Item":{"Name":"ServiceContent","Value":"測試工單"}},{"Item":{"Name":"ContactId","Value":"21072601000000004"}},{"Item":{"Name":"ServiceName","Value":"94"}},{"Item":{"Name":"SRId","Value":"2107260000000011"}},{"Item":{"Name":"MobileNewsNum","Value":"001"}}]}}}
轉換以下變成:
"InterBOSS": {
"SvcCont": {
"SvcRoot": [
{
"Item": {
"Name": "IndictSeq",
"Value": "20210726CSVC2700000003"
}
},
{
"Item": {
"Name": "CallerNo",
"Value": "18271403993"
}
},
{
"Item": {
"Name": "CalledNo",
"Value": "10086"
}
},
{
"Item": {
"Name": "MSISDN",
"Value": "18271403993"
}
},
{
"Item": {
"Name": "SubsName",
"Value": "*樹巨集"
}
},
{
"Item": {
"Name": "SubsLevel",
"Value": "04"
}
},
{
"Item": {
"Name": "SubsBrand",
"Value": "01"
}
},
{
"Item": {
"Name": "SvcTypeId",
"Value": "10010105020101"
}
},
{
"Item": {
"Name": "UrgentId",
"Value": "03"
}
},
{
"Item": {
"Name": "HomeProv",
"Value": "270"
}
},
{
"Item": {
"Name": "SvcCity",
"Value": "027-武漢"
}
},
{
"Item": {
"Name": "TProv",
"Value": "000"
}
},
{
"Item": {
"Name": "AcceptTime",
"Value": "2021-07-26 10:51:40"
}
},
{
"Item": {
"Name": "AcceptStaff",
"Value": "3406"
}
},
{
"Item": {
"Name": "OriginTime",
"Value": "2021-07-26 10:51:40"
}
},
{
"Item": {
"Name": "DeadTime",
"Value": "2021-07-27 10:51:40"
}
},
{
"Item": {
"Name": "Title",
"Value": "test"
}
},
{
"Item": {
"Name": "ProcessType",
"Value": "1"
}
},
{
"Item": {
"Name": "ContactChannel",
"Value": "01"
}
},
{
"Item": {
"Name": "ServiceContent",
"Value": "測試工單"
}
},
{
"Item": {
"Name": "ContactId",
"Value": "21072601000000004"
}
},
{
"Item": {
"Name": "ServiceName",
"Value": "94"
}
},
{
"Item": {
"Name": "SRId",
"Value": "2107260000000011"
}
},
{
"Item": {
"Name": "MobileNewsNum",
"Value": "001"
}
}
]
}
}
},