java JAXB annotation 通用类

import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

/**
 * XmlParserUtil xml与javabean互转
 * 
 */
public class XmlParserUtil
{

    /**
     * Parses the xml to object. 将Xml解析为对应的Object对象.
     * 
     * @param xmlStr
     *            the xml str
     * @param objectClass
     *            the object class
     * @return the object
     * @throws DocumentException
     *             the document exception
     * @throws JAXBException
     *             the jAXB exception
     */
    public static Object parseXmlToObject(String xmlStr, Class<?> objectClass) throws DocumentException, JAXBException
    {
        Document xmldoc = DocumentHelper.parseText(xmlStr);
        return parseXmlToObject(xmldoc, objectClass);
    }

    /**
     * 将Xml解析为对应的Object对象.
     * 
     * @param xmldoc
     *            Document
     * @param objectClass
     *            Class<?> Object的Class
     * @return Object
     * @throws JAXBException
     *             the jAXB exception
     */
    public static Object parseXmlToObject(Document xmldoc, Class<?> objectClass) throws JAXBException
    {
        Object object = null;

        JAXBContext context = JAXBContext.newInstance(objectClass);

        Unmarshaller unmarshaller = context.createUnmarshaller();

        String xmlStr = xmldoc.asXML();

        Reader reader = new StringReader(xmlStr);

        object = unmarshaller.unmarshal(reader);

        return object;

    }

    /**
     * 将Object解析为Xml
     * 
     * @param objectClass
     *            Object对应的Class
     * @param object
     *            Object对象
     * @param out
     *            输出流
     * @throws JAXBException Exception
     */
    public static void parseObjectToXml(Class<?> objectClass, Object object, OutputStream out) throws JAXBException
    {

        JAXBContext context = JAXBContext.newInstance(objectClass);

        Marshaller marshaller = context.createMarshaller();

        // 设置编组属性,使得输出的XML文档进行格式化(提供缩进)
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(object, out);
    }

    /**
     * 
     * * 将Object解析为Xml    
     * 
     * @param objectClass Object对应的Class
     * @param object Object对象
     * @param out 输出流
     * @param encoding 编码
     * @throws JAXBException Exception
     */
    public static void parseObjectToXmlByEncoding(Class<?> objectClass, 
            Object object, OutputStream out, String encoding) throws JAXBException
    {

        JAXBContext context = JAXBContext.newInstance(objectClass);

        Marshaller marshaller = context.createMarshaller();

        // 设置编组属性,使得输出的XML文档进行格式化(提供缩进)
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        marshaller.marshal(object, out);
    }

    /**
     * 将Object解析为Xml.
     * 
     * @param objectClass
     *            Object对应的Class
     * @param object
     *            Object对象
     * @param out
     *            输出流
     * @throws JAXBException
     *             the jAXB exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void parseObjectToXmlNoHeard(Class<?> objectClass, Object object, OutputStream out)
            throws JAXBException, IOException

    {
        JAXBContext context = JAXBContext.newInstance(objectClass);

        Marshaller marshaller = context.createMarshaller();

        // 设置编组属性,使得输出的XML文档进行格式化(提供缩进)
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // 去掉默认的头

        marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);

        // 声明需要的头文件
        String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

        out.write(str.getBytes("UTF-8"));

        marshaller.marshal(object, out);
    }
}

猜你喜欢

转载自fuqiangjava.iteye.com/blog/1816528
今日推荐