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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值