jaxb2实现Javabean转换xml

本文介绍了如何使用JAXB2库将Java Bean转换为XML。通过注解`@XmlRootElement`和`@XmlType`设置XML节点名称和属性顺序,详细展示了两个示例Bean(Root和ObjectSon)的定义。此外,还提供了两个工具类JaxbToXmlUtil和CDataAdapter,前者用于转换对象到XML,后者处理日期格式。在转换过程中,特别注意了XML格式化输出和特殊字符的处理。
代码直接上(1)两个Javabean,Root和ObjectSon此处注意:1.@XmlType下的propOrder是xml排序 应将所有属性添加进去;2.@XmlRootElement下的name是设置节点名称,若果不写,第一个字母会是小写“root”;package com.jaxb.bean;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;@XmlRootElement(name = "Root") @XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "Root", propOrder = {"rootName","rootage","objectSon","cdate"})public class Root {@XmlElementprivate String rootName;@XmlElementprivate String rootage;@XmlElementprivate ObjectSon objectSon;@XmlElementprivate String cdate;public String getRootName() {return rootName;}public void setRootName(String rootName) {this.rootName = rootName;}public String getRootage() {return rootage;}public void setRootage(String rootage) {this.rootage = rootage;}public ObjectSon getObjectSon() {return objectSon;}public void setObjectSon(ObjectSon objectSon) {this.objectSon = objectSon;}public String getCdate() {return cdate;}public void setCdate(String cdate) {this.cdate = cdate;}}package com.jaxb.bean;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;@XmlRootElement(name = "ObjectSon") @XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "ObjectSon", propOrder = {"name","age"})public class ObjectSon {@XmlElementprivate String name;@XmlElementprivate String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}(2)两个工具类JaxbToXmlUtil 和CDataAdapterJaxbToXmlUtil 转换为xml;CDataAdapter 获得CDate 格式package com.jaxb.util;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;public class JaxbToXmlUtil {public static String convertToXml(Object obj, String encoding) {String result = null;try {JAXBContext context = JAXBContext.newInstance(obj.getClass());Marshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);// 去掉生成xml的默认报文头。marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 转换所有的适配字符,包括xml实体字符<和>,xml实体字符在好多处理xml// 的框架中是处理不了的,除非序列化。marshaller.setProperty("com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler",new CharacterEscapeHandler() {@Overridepublic void escape(char[] ch, int start,int length, boolean isAttVal,Writer writer) throws IOException {writer.write(ch, start, length);}});StringWriter writer = new StringWriter();// 添加自己想要的xml报文头writer.write("<?xml version=\'1.0\' encoding=\'" + encoding+ "\'?>\n");marshaller.marshal(obj, writer);result = writer.toString();} catch (JAXBException e) {e.printStackTrace();}return result;}}package com.jaxb.util;import javax.xml.bind.annotation.adapters.XmlAdapter;public class CDataAdapter extends XmlAdapter {@Overridepublic String marshal(String str) throws Exception {return "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值