超級複雜的xml轉map物件,轉json
阿新 • • 發佈:2021-01-25
pom 新增一下依賴
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
工具類
package com.ztd. ytxl.utils;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 處理Xml與Object轉換
* Created by AnZhi_Liu on 2021/1/18 0018 9:57
*/
public class XmlUtil {
/**
* @param xmlStr 字串
* @param c 物件Class型別
* @return 物件例項
*/
@SuppressWarnings("unchecked" )
public static <T> T xml2Object(String xmlStr, Class<T> c) {
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
return t;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
/**
* java 物件轉xml (僅支援含有 @XmlRootElement註解型別物件)
* @param object java 型別物件
* @param hideHeader 是否省略xml頭資訊
* @param format 是否格式化輸出
* @return
*/
public static String object2Xml(Object object,boolean hideHeader,boolean format) {
try {
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); // 格式化輸出
marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 編碼格式,預設為utf-8
marshal.setProperty(Marshaller.JAXB_FRAGMENT, hideHeader);// 是否省略xml頭資訊
marshal.marshal(object, writer);
return new String(writer.getBuffer());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* xml 字串轉 map (支援多級複雜的資料)
* @param xmlStr
* @return
* @throws DocumentException
*/
public static Map<String, Object> xmlToMap(String xmlStr) throws DocumentException {
Map<String, Object> outmap = new HashMap<>();
Element rootElement = DocumentHelper.parseText(xmlStr).getRootElement();
return elementTomap(rootElement, outmap);
}
/**
* XmlToMap核心方法,裡面有遞迴呼叫
* @param outele
* @param outmap
* @return
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> elementTomap(Element outele, Map<String, Object> outmap) {
List<Element> list = outele.elements();
int size = list.size();
if (size == 0) {
outmap.put(outele.getName(), outele.getTextTrim());
} else {
Map<String, Object> innermap = new HashMap<>();
int i = 1;
for (Element ele1 : list) {
String eleName = ele1.getName();
String value = ele1.getText();
Object obj = innermap.get(eleName);
if (obj == null) {
elementTomap(ele1, innermap);
} else {
if (obj instanceof java.util.Map) {
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
list1.add((Map<String, Object>) innermap.remove(eleName));
elementTomap(ele1, innermap);
list1.add((Map<String, Object>) innermap.remove(eleName));
innermap.put(eleName, list1);
} else if (obj instanceof String) {
innermap.put(eleName + i, value);
i++;
} else {
elementTomap(ele1, innermap);
Map<String, Object> listValue = (Map<String, Object>) innermap.get(eleName);
((List<Map<String, Object>>) obj).add(listValue);
innermap.put(eleName, obj);
}
}
}
outmap.put(outele.getName(), innermap);
}
return outmap;
}
}