Java tutorial
/** * Copyright (c) 2005-2011 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: JaxbMapper.java 1591 2011-05-11 01:42:15Z calvinxiu $ */ package cn.hxh.springside.mapper; import java.io.StringReader; import java.io.StringWriter; import java.util.Collection; 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.lang.StringUtils; import cn.hxh.springside.utils.ExceptionUtils; /** * Jaxb2.0XML<->Java ObjectMapper. * * ???RootClass. * ?RootCollection. * * @author calvin */ public class JaxbMapper { //Context. private JAXBContext jaxbContext; /** * @param rootTypes ??RootClass. */ public JaxbMapper(Class<?>... rootTypes) { try { jaxbContext = JAXBContext.newInstance(rootTypes); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } } /** * Java Object->Xml without encoding. */ public String toXml(Object root) { return toXml(root, null); } /** * Java Object->Xml with encoding. */ public String toXml(Object root, String encoding) { try { StringWriter writer = new StringWriter(); createMarshaller(encoding).marshal(root, writer); return writer.toString(); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } } /** * Java Object->Xml without encoding, ?Root ElementCollection. */ public String toXml(Collection<?> root, String rootName) { return toXml(root, rootName, null); } /** * Java Object->Xml with encoding, ?Root ElementCollection. */ public String toXml(Collection<?> root, String rootName, 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(encoding).marshal(wrapperElement, writer); return writer.toString(); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } } /** * Xml->Java Object. */ public <T> T fromXml(String xml) { try { StringReader reader = new StringReader(xml); return (T) createUnmarshaller().unmarshal(reader); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } } /** * Marshallerencoding(?null). */ public Marshaller createMarshaller(String encoding) { try { 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) { throw ExceptionUtils.unchecked(e); } } /** * UnMarshaller. */ public Unmarshaller createUnmarshaller() { try { return jaxbContext.createUnmarshaller(); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } } /** * ?Root Element Collection. */ public static class CollectionWrapper { @XmlAnyElement protected Collection<?> collection; } }