XML转成JSON带属性:
XML文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope>
<ItemFeeAdjustments>
<Fee>
<Type>Commission</Type>
<Amount currency="EUR">1.92</Amount>
</Fee>
<Fee>
<Type>RefundCommission</Type>
<Amount currency="EUR">-0.38</Amount>
</Fee>
</ItemFeeAdjustments>
</AmazonEnvelope>
引入包:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.55</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jdom/jdom2 -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
1.属性转化为同级
package util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class Xml2JsonUtil {
private static void xml2Json(Element element, JSONObject jsonObject) {
List<Element> childElements = element.getChildren();
if (childElements.isEmpty()) {// 如果没有子元素,只有一个值
//解析属性
List<Attribute> attributeList = element.getAttributes();
if (attributeList != null && !attributeList.isEmpty()) {
for (Attribute attr : attributeList) {
jsonObject.put(attr.getName(), attr.getValue());
}
}
jsonObject.put(element.getName(), element.getValue());
} else {
for (Element node : childElements) {// 有子元素
if (node.getChildren() != null && !node.getChildren().isEmpty()) {// 子元素也有子元素
JSONObject childJsonObject = new JSONObject();
xml2Json(node, childJsonObject);
Object object = jsonObject.get(node.getName());
if (object != null) {
JSONArray jsonArray = null;
if (object instanceof JSONObject) {// 如果此元素已存在,则转为jsonArray
JSONObject jsono = (JSONObject) object;
jsonObject.remove(node.getName());
jsonArray = new JSONArray();
jsonArray.add(jsono);
jsonArray.add(childJsonObject);
}
if (object instanceof JSONArray) {
jsonArray = (JSONArray) object;
jsonArray.add(childJsonObject);
}
jsonObject.put(node.getName(), jsonArray);
} else {
if (!childJsonObject.isEmpty()) {
jsonObject.put(node.getName(), childJsonObject);
}
}
} else {// 子元素没有子元素
//解析属性
List<Attribute> attributeList = node.getAttributes();
if (attributeList != null && !attributeList.isEmpty()) {
for (Attribute attr : attributeList) {
jsonObject.put(attr.getName(), attr.getValue());
}
}
jsonObject.put(node.getName(), node.getValue());
}
}
}
}
public static void xml2JSON(InputStream inputStream) {
JSONObject obj = new JSONObject();
try {
SAXBuilder saxBuilder = new SAXBuilder();
//2创建一个流
InputStreamReader isr = new InputStreamReader(inputStream);
// 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
Document document = saxBuilder.build(isr);
// 4.通过document对象获取xml文件的根节点
Element rootElement = document.getRootElement();
JSONObject jsonObject = new JSONObject();
xml2Json(rootElement, jsonObject);
System.out.println(jsonObject.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//调用
File file = new File("C:\\Users\\52608\\Desktop\\shuzu.xml");
try {
InputStream inputStream = new FileInputStream(file);
Xml2JsonUtil.xml2JSON(inputStream);
} catch (Exception e) {
System.out.println("error");
}
执行结果:
{
"ItemFeeAdjustments": {
"Fee": [{
"Type": "Commission",
"Amount": "1.92",
"currency": "EUR"
}, {
"Type": "RefundCommission",
"Amount": "-0.38",
"currency": "EUR"
}]
}
}
2.属性转化为子级
package util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class Xml2JsonUtil {
private static void xml2Json(Element element, JSONObject jsonObject) {
List<Element> childElements = element.getChildren();
if (childElements.isEmpty()) {// 如果没有子元素,只有一个值
//解析属性
List<Attribute> attributeList = element.getAttributes();
if (attributeList != null && !attributeList.isEmpty()) {
JSONObject attrObject = new JSONObject();
for (Attribute attr : attributeList) {
attrObject.put(attr.getName(), attr.getValue());
}
attrObject.put("value", element.getValue());
jsonObject.put(element.getName(), attrObject);
} else {
jsonObject.put(element.getName(), element.getValue());
}
} else {
for (Element node : childElements) {// 有子元素
if (node.getChildren() != null && !node.getChildren().isEmpty()) {// 子元素也有子元素
JSONObject childJsonObject = new JSONObject();
xml2Json(node, childJsonObject);
Object object = jsonObject.get(node.getName());
if (object != null) {
JSONArray jsonArray = null;
if (object instanceof JSONObject) {// 如果此元素已存在,则转为jsonArray
JSONObject jsono = (JSONObject) object;
jsonObject.remove(node.getName());
jsonArray = new JSONArray();
jsonArray.add(jsono);
jsonArray.add(childJsonObject);
}
if (object instanceof JSONArray) {
jsonArray = (JSONArray) object;
jsonArray.add(childJsonObject);
}
jsonObject.put(node.getName(), jsonArray);
} else {
if (!childJsonObject.isEmpty()) {
jsonObject.put(node.getName(), childJsonObject);
}
}
} else {// 子元素没有子元素
//解析属性
List<Attribute> attributeList = node.getAttributes();
if (attributeList != null && !attributeList.isEmpty()) {
JSONObject attrObject = new JSONObject();
for (Attribute attr : attributeList) {
attrObject.put(attr.getName(), attr.getValue());
}
attrObject.put("value", node.getValue());
jsonObject.put(node.getName(), attrObject);
} else {
jsonObject.put(node.getName(), node.getValue());
}
}
}
}
}
public static void xml2JSON(InputStream inputStream) {
JSONObject obj = new JSONObject();
try {
SAXBuilder saxBuilder = new SAXBuilder();
//2创建一个流
InputStreamReader isr = new InputStreamReader(inputStream);
// 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
Document document = saxBuilder.build(isr);
// 4.通过document对象获取xml文件的根节点
Element rootElement = document.getRootElement();
JSONObject jsonObject = new JSONObject();
xml2Json(rootElement, jsonObject);
System.out.println(jsonObject.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//调用
File file = new File("D:\\shuzu.xml");
try {
InputStream inputStream = new FileInputStream(file);
Xml2JsonUtil.xml2JSON(inputStream);
} catch (Exception e) {
System.out.println("error");
}
转化结果:
{
"ItemFeeAdjustments": {
"Fee": [{
"Type": "Commission",
"Amount": {
"currency": "EUR",
"value": "1.92"
}
}, {
"Type": "RefundCommission",
"Amount": {
"currency": "EUR",
"value": "-0.38"
}
}]
}
}