转载自:https://www.jianshu.com/p/e341c12449d2
自己debug跟源码,主要解决其中json转xml遇到的一些问题
1. 依赖引入
1 <dependency> 2 <groupId>net.sf.json-lib</groupId> 3 <artifactId>json-lib</artifactId> 4 <version>2.4</version> 5 <classifier>jdk15</classifier> 6 </dependency> 7 8 <dependency> 9 <groupId>xom</groupId> 10 <artifactId>xom</artifactId> 11 <version>1.2.5</version> 12 </dependency>
2. Xml 2Json案例
1 <?xml version="1.0" encoding="utf-8"?> 2 <Mk_Main_Info Mk_Id="本地系统中的 ID" Mk_Remarke="" Mk_Table="单位名称" Mk_Version="Ver 3.00.0001"> 3 <Mk_Item Mk_Row="行号"> 4 <Mk_Item_Data Mk_Field="fg_dwbm"> 5 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 6 </Mk_Item_Data> 7 <Mk_Item_Data Mk_Field="fg_dwbc"> 8 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 9 </Mk_Item_Data> 10 </Mk_Item> 11 </Mk_Main_Info>
1 public static void main(String[] args) { 2 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Mk_Main_Info Mk_Id=\"本地系统中的 ID\" Mk_Version=\"Ver 3.00.0001\" Mk_Table=\"单位名称\" Mk_Remarke=\"\">\n" + "\t\t<Mk_Item Mk_Row=\"行号\">\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbm\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbc\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t</Mk_Item>\n" + "\t</Mk_Main_Info>"; 3 System.out.println("==========xml to json================"); 4 XMLSerializer xmlSerializer = new XMLSerializer(); 5 // TODO 注意:默认false:不输出根元素, true为输出根元素 6 xmlSerializer.setForceTopLevelObject(true); 7 JSON read = xmlSerializer.read(xml); 8 String jsonStr = read.toString(); 9 // jsonStr.replace("[]", "\"\""); 10 System.out.println(jsonStr); 11 }
1 { 2 "Mk_Main_Info": { 3 "@Mk_Id": "本地系统中的 ID", 4 "@Mk_Version": "Ver 3.00.0001", 5 "@Mk_Table": "单位名称", 6 "@Mk_Remarke": "", 7 "Mk_Item": { 8 "@Mk_Row": "行号", 9 "Mk_Item_Data": [ 10 { 11 "@Mk_Field": "fg_dwbm", 12 "Mk_Data": "系统中编码[连接数据时提供]" 13 }, 14 { 15 "@Mk_Field": "fg_dwbc", 16 "Mk_Data": "系统中编码[连接数据时提供]" 17 } 18 ] 19 } 20 } 21 }
- Json2Xml
直接根据上面的输出结果
1 public static void main(String[] args) { 2 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Mk_Main_Info Mk_Id=\"本地系统中的 ID\" Mk_Version=\"Ver 3.00.0001\" Mk_Table=\"单位名称\" Mk_Remarke=\"\">\n" + "\t\t<Mk_Item Mk_Row=\"行号\">\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbm\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbc\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t</Mk_Item>\n" + "\t</Mk_Main_Info>"; 3 System.out.println("==========xml to json================"); 4 XMLSerializer xmlSerializer = new XMLSerializer(); 5 // 默认false:不输出根元素, true为输出根元素 6 xmlSerializer.setForceTopLevelObject(true); 7 JSON read = xmlSerializer.read(xml); 8 String jsonStr = read.toString(); 9 // jsonStr.replace("[]", "\"\""); 10 System.out.println(jsonStr); 11 System.out.println("==========json to xml================"); 12 String write = xmlSerializer.write(JSONSerializer.toJSON(jsonStr), "gbk"); 13 System.out.println(write); 14 }
1 <?xml version="1.0" encoding="gbk"?> 2 <o> 3 <Mk_Main_Info class="object" Mk_Id="本地系统中的 ID" Mk_Remarke="" Mk_Table="单位名称" Mk_Version="Ver 3.00.0001"> 4 <Mk_Item class="object" Mk_Row="行号"> 5 <Mk_Item_Data class="array"> 6 <e class="object" Mk_Field="fg_dwbm"> 7 <Mk_Data type="string">系统中编码[连接数据时提供]</Mk_Data> 8 </e> 9 <e class="object" Mk_Field="fg_dwbc"> 10 <Mk_Data type="string">系统中编码[连接数据时提供]</Mk_Data> 11 </e> 12 </Mk_Item_Data> 13 </Mk_Item> 14 </Mk_Main_Info> 15 </o>
莫名其妙的多个不想要的元素‘o’,'e',以及属性class="object"
3.1 去掉class="object"
1 public static void main(String[] args) { 2 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Mk_Main_Info Mk_Id=\"本地系统中的 ID\" Mk_Version=\"Ver 3.00.0001\" Mk_Table=\"单位名称\" Mk_Remarke=\"\">\n" + "\t\t<Mk_Item Mk_Row=\"行号\">\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbm\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbc\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t</Mk_Item>\n" + "\t</Mk_Main_Info>"; 3 System.out.println("==========xml to json================"); 4 XMLSerializer xmlSerializer = new XMLSerializer(); 5 // 默认false:不输出根元素, true为输出根元素 6 xmlSerializer.setForceTopLevelObject(true); 7 JSON read = xmlSerializer.read(xml); 8 String jsonStr = read.toString(); 9 System.out.println(jsonStr); 10 System.out.println("==========json to xml================"); 11 // TODO 注意: <Mk_Item class="object" Mk_Row="行号"> 类型提示 true:展示,false:不展示 12 xmlSerializer.setTypeHintsEnabled(false); 13 String write = xmlSerializer.write(JSONSerializer.toJSON(jsonStr), "gbk"); 14 System.out.println(write); 15 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <o> 3 <Mk_Main_Info Mk_Id="本地系统中的 ID" Mk_Remarke="" Mk_Table="单位名称" Mk_Version="Ver 3.00.0001"> 4 <Mk_Item Mk_Row="行号"> 5 <Mk_Item_Data> 6 <e Mk_Field="fg_dwbm"> 7 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 8 </e> 9 <e Mk_Field="fg_dwbc"> 10 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 11 </e> 12 </Mk_Item_Data> 13 </Mk_Item> 14 </Mk_Main_Info> 15 </o>
3.2 去掉元素 e
1 // 将jsonArray 的元素,去掉父节点 ‘e’ String[] expandableProperties = {"Mk_Item_Data"}; 2 xmlSerializer.setExpandableProperties(expandableProperties);
1 <?xml version="1.0" encoding="utf-8"?> 2 <o> 3 <Mk_Main_Info Mk_Id="本地系统中的 ID" Mk_Remarke="" Mk_Table="单位名称" Mk_Version="Ver 3.00.0001"> 4 <Mk_Item Mk_Row="行号"> 5 <Mk_Item_Data Mk_Field="fg_dwbm"> 6 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 7 </Mk_Item_Data> 8 <Mk_Item_Data Mk_Field="fg_dwbc"> 9 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 10 </Mk_Item_Data> 11 </Mk_Item> 12 </Mk_Main_Info> 13 </o>
3.3 去掉元素o
这步需要注意,我们由xml生成的json是包括根元素的,我们可以在生成json的时候不生成根元素即 xmlSerializer.setForceTopLevelObject(false)
再由生成的json字符串为入参,设置 xmlSerializer.setRootName("Mk_Main_Info")
1 public static void main(String[] args) { 2 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Mk_Main_Info Mk_Id=\"本地系统中的 ID\" Mk_Version=\"Ver 3.00.0001\" Mk_Table=\"单位名称\" Mk_Remarke=\"\">\n" + "\t\t<Mk_Item Mk_Row=\"行号\">\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbm\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t\t<Mk_Item_Data Mk_Field=\"fg_dwbc\">\n" + "\t\t\t\t<Mk_Data>系统中编码[连接数据时提供]</Mk_Data>\n" + "\t\t\t</Mk_Item_Data>\n" + "\t\t</Mk_Item>\n" + "\t</Mk_Main_Info>"; 3 JSONObject jsonObject = XML.toJSONObject(xml); 4 String jsonPrettyPrintString = jsonObject.toString(2); 5 System.out.println(jsonPrettyPrintString); 6 System.out.println("==========xml to json================"); 7 XMLSerializer xmlSerializer = new XMLSerializer(); 8 // true:输出根元素 9 xmlSerializer.setForceTopLevelObject(false); 10 JSON read = xmlSerializer.read(xml); 11 String jsonStr = read.toString(); 12 System.out.println(jsonStr); 13 System.out.println("+++++++++++json to xml+++++++++++++"); 14 // 问题1:<Mk_Item class="object" Mk_Row="行号"> 类型提示 true:展示,false:不展示 15 xmlSerializer.setTypeHintsEnabled(false); 16 // 问题2:将jsonArray 的元素,去掉父节点 ‘e’ 17 String[] expandableProperties = {"Mk_Item_Data"}; 18 xmlSerializer.setExpandableProperties(expandableProperties); 19 // 问题3:去掉根节点 ‘o’ 20 xmlSerializer.setRootName("Mk_Main_Info"); // 自定义根元素 21 String write = xmlSerializer.write(JSONSerializer.toJSON(jsonStr), "utf-8"); 22 System.out.println(write); 23 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <Mk_Main_Info Mk_Id="本地系统中的 ID" Mk_Remarke="" Mk_Table="单位名称" Mk_Version="Ver 3.00.0001"> 3 <Mk_Item Mk_Row="行号"> 4 <Mk_Item_Data Mk_Field="fg_dwbm"> 5 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 6 </Mk_Item_Data> 7 <Mk_Item_Data Mk_Field="fg_dwbc"> 8 <Mk_Data>系统中编码[连接数据时提供]</Mk_Data> 9 </Mk_Item_Data> 10 </Mk_Item> 11 </Mk_Main_Info>