项目背景:
返回的数据格式:
<?xml version="1.0" encoding="GBK">
<root>
<head>
<code>1</code>
<message>数据下载成功</message>
<rownum>1</rownum>
</head>
<body>
<person id="0">
<name>张三</name>
<age>18</age>
</person>
</body>
</root>
<?xml version="1.0" encoding="GBK">
<root>
<head>
<code>1</code>
<message>数据下载成功</message>
<rownum>1</rownum>
</head>
<body>
<pet id="0">
<name>企鹅</name>
<age>2</age>
</person>
</body>
</root>
现在需要把body里面的数据取出来返回的数据格式为List<Map<String, String>> list,map里面存放属性名为key,值为value
写一个公共的方法处理里面的xml
/**
* @param xmlBody 返回的xml字符串
* @param xmlClass xml中返回消息节点名字对应的java类
* @return java.util.List<java.util.HashMap<java.lang.String,java.lang.String>>
* @author
* @createTime 2022/10/19 10:00
* @description
*/
private static List<HashMap<String, String>> delXmlContent(String xmlBody, Class<?> xmlClass) {
JAXBContext jc;
Object unmarshal = null;
try {
jc = JAXBContext.newInstance(xmlClass);
MapAdapter mapAdapter = new MapAdapter();
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(mapAdapter);
unmarshal = unmarshaller.unmarshal(new StringReader(xmlBody));
} catch (Exception e) {
_LOGGER.error(e.getMessage(), e);
}
// 通过反射获取对象里属性的值
Class<?> aClass = unmarshal.getClass();
Object value = null;
try {
Method getVariableMap = aClass.getMethod("getVariableMap");
value = getVariableMap.invoke(unmarshal);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
_LOGGER.error(e.getMessage(), e);
}
return (List<HashMap<String, String>>) value;
}
再写一个方法使用
需要创建一个java类来保存解析后的数据
PersonXmlContent(里面的注解要对应返回的xml标签的名字)
@XmlRootElement(name = "body")
@XmlAccessorType(XmlAccessType.FIELD)
public class DriverXmlContent {
// body标签的属性
@XmlAttribute
private int id;
// body里面的子标签
@XmlElement(name = "person")
// 如果需要转换成map需要自定义适配器
@XmlJavaTypeAdapter(MapAdapter.class)
private List<HashMap<String, String>> variableMap;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<HashMap<String, String>> getVariableMap() {
return variableMap;
}
public void setVariableMap(List<HashMap<String, String>> variableMap) {
this.variableMap = variableMap;
}
}
MapAdapter继承XmlAdapter在xml解析的时候对数据做处理
public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, String>> {
private DocumentBuilder documentBuilder;
public MapAdapter() throws Exception {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
public static class AdaptedMap {
@XmlAnyElement
public List<Element> elements = new ArrayList<Element>();
}
@Override
public AdaptedMap marshal(Map<String, String> map) throws Exception {
Document document = documentBuilder.newDocument();
AdaptedMap adaptedMap = new AdaptedMap();
for(Map.Entry<String, String> entry : map.entrySet()) {
Element element = document.createElement(entry.getKey());
element.setTextContent(entry.getValue());
adaptedMap.elements.add(element);
}
return adaptedMap;
}
@Override
public Map<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
for(Element element : adaptedMap.elements) {
map.put(element.getLocalName(), element.getTextContent());
}
return map;
}
}
使用
public static void main(String[] args) {
// 如果body里是其他的标签,只要新建一个java类,在属性注解上把节点的名字对应上,再把java类传到公共方法就可以了
List<HashMap<String, String>> hashMaps = delXmlContent(xmlString, VehXmlContent.class);
}