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