Jaxb2.0实现Java Object转换Xml转换Java Object.

//测试类
import com.toft.webservice.client.domino.DominoObject;
import com.toft.webservice.core.JaxbBinder;


public class JaxbTest {
	
	private static final String DECLARATION = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE document SYSTEM 'xmlschemas/domino_7_0_1.dtd'>";
	private static final String ENCODING = "UTF-8";
	private DominoObject dominoObject = new DominoObject();
	public static void main(String[] args){
		
	}
	
	/**
	 *  使用JAXB生成符合接口的XML.
	 * @return String
	 * @throws Exception
	 */
	private String getDominoXml() throws Exception {
		try {
			JaxbBinder binder = new JaxbBinder(DominoObject.class);
			return binder.toXml(dominoObject, ENCODING, DECLARATION);
		} catch (Exception e) {
			throw e;
		}
	}
}


 
//工具类
import java.io.StringReader;
import java.io.StringWriter;

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

/**
 * 使用Jaxb2.0实现XML<->Java Object的Binder.
 * 
 * @author 孙祖强
 */
public class JaxbBinder {
	//多线程安全的Context.
	private JAXBContext jaxbContext;

	/**
	 * @param types 所有需要序列化的Root对象的类型.
	 */
	public JaxbBinder(Class<?>... types) {
		try {
			jaxbContext = JAXBContext.newInstance(types);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Java Object->Xml.
	 */
	public String toXml(Object root) {
		try {
			StringWriter writer = new StringWriter();
			createMarshaller("UTF-8", null, true).marshal(root, writer);
			return writer.toString();
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Java Object->Xml.
	 */
	public String toXml(Object root, String encoding) {
		try {
			StringWriter writer = new StringWriter();
			createMarshaller(encoding, null, false).marshal(root, writer);
			return writer.toString();
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Java Object->Xml.
	 */
	public String toXml(Object root, String encoding, String declaration) {
		try {
			StringWriter writer = new StringWriter();
			writer.append(declaration);
			createMarshaller(encoding, declaration, false).marshal(root, writer);
			return writer.toString();
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Xml->Java Object.
	 */
	@SuppressWarnings("unchecked")
	public <T> T fromXml(String xml) {
		try {
			StringReader reader = new StringReader(xml);
			return (T) createUnmarshaller().unmarshal(reader);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 创建Marshaller, 设定encoding(可为Null),  设定declaration(可为Null).
	 */
	public Marshaller createMarshaller(String encoding, String declaration, Boolean formatted) {
		try {
			Marshaller marshaller = jaxbContext.createMarshaller();

			//是否格式化XML
			if (formatted) {
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			}

			//设置编码方式
			if (null != encoding && !"".equals(encoding)) {
				marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
			}

			//设置XML声明
			if (null != declaration && !"".equals(declaration)) {
				marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
			}
			return marshaller;
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 创建UnMarshaller.
	 */
	public Unmarshaller createUnmarshaller() {
		try {
			return jaxbContext.createUnmarshaller();
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值