java to xml_JAVA实体XML相互转换

packagecom.jiayu.his.util;importjava.io.StringReader;importjava.io.StringWriter;importjava.util.Collection;importjava.util.concurrent.ConcurrentHashMap;importjava.util.concurrent.ConcurrentMap;importjavax.xml.bind.JAXBContext;importjavax.xml.bind.JAXBElement;importjavax.xml.bind.JAXBException;importjavax.xml.bind.Marshaller;importjavax.xml.bind.Unmarshaller;importjavax.xml.bind.annotation.XmlAnyElement;importjavax.xml.namespace.QName;importorg.apache.commons.lang3.StringUtils;importorg.apache.commons.lang3.Validate;/*** 使用Jaxb2.0实现XMLJava Object的Mapper.

* 在创建时需要设定所有需要序列化的Root对象的Class.

* 特别支持Root对象是Collection的情形.*/

public classJaxbUtil {

@SuppressWarnings("rawtypes")private static ConcurrentMap jaxbContexts = new ConcurrentHashMap();/*** Java Object->Xml without encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root) {

Class clazz=Reflections.getUserClass(root);return toXml(root, clazz, null);

}/*** Java Object->Xml with encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root, String encoding) {

Class clazz=Reflections.getUserClass(root);returntoXml(root, clazz, encoding);

}/*** Java Object->Xml with encoding.*/@SuppressWarnings("rawtypes")public staticString toXml(Object root, Class clazz, String encoding) {try{

StringWriter writer= newStringWriter();

createMarshaller(clazz, encoding).marshal(root, writer);returnwriter.toString();

}catch(JAXBException e) {throwExceptions.unchecked(e);

}

}/*** Java Collection->Xml without encoding, 特别支持Root Element是Collection的情形.*/@SuppressWarnings("rawtypes")public static String toXml(Collection>root, String rootName, Class clazz) {return toXml(root, rootName, clazz, null);

}/*** Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.*/@SuppressWarnings("rawtypes")public static String toXml(Collection>root, String rootName, Class clazz, String encoding) {try{

CollectionWrapper wrapper= newCollectionWrapper();

wrapper.collection=root;

JAXBElement wrapperElement = new JAXBElement(newQName(rootName),

CollectionWrapper.class, wrapper);

StringWriter writer= newStringWriter();

createMarshaller(clazz, encoding).marshal(wrapperElement, writer);returnwriter.toString();

}catch(JAXBException e) {throwExceptions.unchecked(e);

}

}/*** Xml->Java Object.*/@SuppressWarnings("unchecked")public static T fromXml(String xml, Classclazz) {try{

StringReader reader= newStringReader(xml);return(T) createUnmarshaller(clazz).unmarshal(reader);

}catch(JAXBException e) {throwExceptions.unchecked(e);

}

}/*** 创建Marshaller并设定encoding(可为null).

* 线程不安全,需要每次创建或pooling。*/@SuppressWarnings("rawtypes")public staticMarshaller 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);

}returnmarshaller;

}catch(JAXBException e) {throwExceptions.unchecked(e);

}

}/*** 创建UnMarshaller.

* 线程不安全,需要每次创建或pooling。*/@SuppressWarnings("rawtypes")public staticUnmarshaller createUnmarshaller(Class clazz) {try{

JAXBContext jaxbContext=getJaxbContext(clazz);returnjaxbContext.createUnmarshaller();

}catch(JAXBException e) {throwExceptions.unchecked(e);

}

}

@SuppressWarnings("rawtypes")protected staticJAXBContext 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);

}

}returnjaxbContext;

}/*** 封装Root Element 是 Collection的情况.*/

public static classCollectionWrapper {

@XmlAnyElementprotected Collection>collection;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值