Java中Xml和实体互转
1. 工具类 JaxbUtil
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.namespace.QName;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import sun.awt.geom.AreaOp;
public class JaxbUtil {
@SuppressWarnings("rawtypes")
private static ConcurrentMap<Class, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class, JAXBContext>();
@SuppressWarnings("rawtypes")
public static String toXml(Object root) {
Class clazz = root.getClass();
return toXml(root, clazz, null);
}
@SuppressWarnings("rawtypes")
public static String toXml(Object root, String encoding) {
Class clazz = root.getClass();
return toXml(root, clazz, encoding);
}
@SuppressWarnings("rawtypes")
public static String toXml(Object root, Class clazz, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(clazz, encoding).marshal(root, writer);
return writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
public static String toXml(Collection<?> root, String rootName, Class clazz) {
return toXml(root, rootName, clazz, null);
}
@SuppressWarnings("rawtypes")
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
try {
CollectionWrapper wrapper = new CollectionWrapper();
wrapper.collection = root;
JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
CollectionWrapper.class, wrapper);
StringWriter writer = new StringWriter();
createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
return writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public static <T> T fromXml(String xml, Class<T> clazz) {
try {
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller(clazz).unmarshal(reader);
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
public static Marshaller createMarshaller(Class clazz, String encoding) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (StringUtils.isNotBlank(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
public static Unmarshaller createUnmarshaller(Class clazz) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
protected static JAXBContext getJaxbContext(Class clazz) {
Validate.notNull(clazz, "'clazz' must not be null");
JAXBContext jaxbContext = jaxbContexts.get(clazz);
if (jaxbContext == null) {
try {
jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
jaxbContexts.putIfAbsent(clazz, jaxbContext);
} catch (JAXBException ex) {
throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: "
+ ex.getMessage(), ex);
}
}
return jaxbContext;
}
public static class CollectionWrapper {
@XmlAnyElement
protected Collection<?> collection;
}
}
2. 实体转Xml演示
2-1 实体类,Main方法中,含实体和Xml互转的测试代码
package com.ruoyi.system.controller;
import com.alibaba.fastjson.JSON;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement(name="sendHospitalInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Hospital implements Serializable {
@XmlElement(name="hospitalId")
private String hospitalid;
@XmlTransient
private long id;
@XmlElement(name="hospitalName")
private String hospitalname;
public String getHospitalid() {
return hospitalid;
}
public void setHospitalid(String hospitalid) {
this.hospitalid = hospitalid;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getHospitalname() {
return hospitalname;
}
public void setHospitalname(String hospitalname) {
this.hospitalname = hospitalname;
}
public static void main(String[] args) {
Hospital sydw = new Hospital();
sydw.setHospitalid("001");
sydw.setHospitalname("hospital_name");
Hospital sydw2 = new Hospital();
sydw2.setHospitalid("002");
sydw2.setHospitalname("hospital_name");
List<Hospital> lists = new ArrayList<>();
lists.add(sydw);
lists.add(sydw2);
Demo demo = new Demo();
demo.setHospitals(lists);
System.out.println(JaxbUtil.toXml(demo));
System.out.println(JaxbUtil.toXml(sydw));
Demo demo1 = new Demo();
demo1=JaxbUtil.fromXml(JaxbUtil.toXml(demo),Demo.class);
System.out.println(JSON.toJSONString(demo1));
}
}
@XmlRootElement(name="sendHospitalInfoList")
@XmlAccessorType(XmlAccessType.FIELD)
class Demo{
private List<Hospital> hospitals;
public List<Hospital> getHospitals() {
return hospitals;
}
public void setHospitals(List<Hospital> hospitals) {
this.hospitals = hospitals;
}
}
2-2 生成的Xml结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sendHospitalInfoList>
<hospitals>
<hospitalId>001</hospitalId>
<hospitalName>hospital_name</hospitalName>
</hospitals>
<hospitals>
<hospitalId>002</hospitalId>
<hospitalName>hospital_name</hospitalName>
</hospitals>
</sendHospitalInfoList>