目录
前言
xml文件转JSON文件与xml字符串转JSON字符串没有太多差别,唯一的区别在于多了一步文件的读写。
在过程中使用了json文件和xml文件转字符串的使用到了IO流。在这里引入了一个maven jar包简化代码(偷个懒)
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency>
文件转字符串代码
FileUtils.readFileToString(file,StandardCharsets.UTF_8);
xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>hello</name>
<age>11</age>
<aas>
<aa>
<name >hello</name>
<age>111</age>
</aa>
<aa>
<name>hello</name>
<age>112</age>
</aa>
<aa>
<name>hello</name>
<age>113</age>
</aa>
</aas>
</root>
xml字符串转JSON字符串
1 第一种方式:引入json包
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
使用代码
JSONObject jsonObject = XML.toJSONObject(xmlStr);
执行结果:
{
"root": {
"aas": {
"aa": [
{
"name": "hello",
"age": 111
},
{
"name": "hello",
"age": 112
},
{
"name": "hello",
"age": 113
}
]
},
"name": "hello",
"age": 11
}
}
2 第二种方式:引入json-lib包
注意:
1、xom依赖包必须添加不然执行会报错
2、json-lib引包时<classifier>jdk15</classifier>必须写,不然无法使用
<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.3.7</version>
</dependency>
<dependency>
使用代码:
XMLSerializer xmlSerializer = new XMLSerializer();
JSON read = xmlSerializer.read(xmlStr);
String content = read.toString();
执行结果:
{
"name": "hello",
"age": "11",
"aas": [
{
"name": "hello",
"age": "111"
},
{
"name": "hello",
"age": "112"
},
{
"name": "hello",
"age": "113"
}
]
}
3 第三种方式:引入dom4j包
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
代码:
/**
* dom2j 解析xml转JSON
* @param xmlStr
* @return
* @throws DocumentException
*/
public static String dom2j_xmlToJson(String xmlStr) throws DocumentException {
Document document = DocumentHelper.parseText(xmlStr);
JSONObject jsonObject =new JSONObject();
dom4j2Json(document.getRootElement(),jsonObject);
return jsonObject.toString();
}
/**
* 通过dom4j2解析xml转JSON
* @param rootElement root节点
* @param jsonObject JSON对象
*/
private static void dom4j2Json(Element rootElement, JSONObject jsonObject) {
//属性处理
for (int i = 0; i < rootElement.attributes().size(); i++) {
Attribute attribute = rootElement.attribute(i);
jsonObject.put("@"+attribute.getName(),attribute.getValue());
}
List<Element> elements = rootElement.elements();
if(elements.isEmpty()&& StringUtils.isNotEmpty(rootElement.getText())){
jsonObject.put(rootElement.getName(),rootElement.getData());
}
for (int i = 0; i < elements.size(); i++) {
Element element = elements.get(i);
if(element.elements().isEmpty()){
for (int i1 = 0; i1 < element.attributes().size(); i1++) {
Attribute attribute = element.attribute(i1);
jsonObject.put("@"+attribute.getName(),attribute.getValue());
}
if(!element.getText().isEmpty()){
jsonObject.put(element.getName(), element.getData());
}
}else{
JSONObject childJson = new JSONObject();
dom4j2Json(element,childJson);
Object o = jsonObject.get(element.getName());
if(o!=null){
com.alibaba.fastjson.JSONArray jsonArray = null;
if(o instanceof JSONObject){
jsonArray = new JSONArray();
jsonArray.add(o);
jsonArray.add(childJson);
}
if(o instanceof JSONArray){
jsonArray = (JSONArray) o;
jsonArray.add(childJson);
}
jsonObject.put(element.getName(),jsonArray);
}else{
if(!childJson.isEmpty()){
jsonObject.put(element.getName(),childJson);
}
}
}
}
}
执行结果:
{
"aas": {
"aa": [{
"name": "hello",
"age": "111"
}, {
"name": "hello",
"age": "112"
}, {
"name": "hello",
"age": "113"
}],
"@type": "array"
},
"name": "hello",
"age": "11"
}