使用JAXB实现JAVA对象和XML字符串的互相转换
2016-07-17 18:35
986 查看
import java.util.ArrayList;
import java.util.List;
import com.yanek.test.JaxbUtil.CollectionWrapper;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//创建java对象
Hotel hotel=new Hotel();
hotel.setId(1);
hotel.setName("name1");
RoomTypeVO t1=new RoomTypeVO();
t1.setPrice("20");
t1.setTypeid(1);
t1.setTypename("typename1");
RoomTypeVO t2=new RoomTypeVO();
t2.setPrice("30");
t2.setTypeid(2);
t2.setTypename("typename2");
List<RoomTypeVO> RoomTypeVOs=new ArrayList<RoomTypeVO>();
RoomTypeVOs.add(t1);
RoomTypeVOs.add(t2);
hotel.setRoomTypeVOs(RoomTypeVOs);
//将java对象转换为XML字符串
JaxbUtil requestBinder = new JaxbUtil(Hotel.class,
CollectionWrapper.class);
String retXml = requestBinder.toXml(hotel, "utf-8");
System.out.println("xml:"+retXml);
//将xml字符串转换为java对象
JaxbUtil resultBinder = new JaxbUtil(Hotel.class,
CollectionWrapper.class);
Hotel hotelObj = resultBinder.fromXml(retXml);
System.out.println("hotelid:"+hotelObj.getId());
System.out.println("hotelname:"+hotelObj.getName());
for(RoomTypeVO roomTypeVO:hotelObj.getRoomTypeVOs())
{
System.out.println("Typeid:"+roomTypeVO.getTypeid());
System.out.println("Typename:"+roomTypeVO.getTypename());
}
}
实体类:import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "hotel")
public class Hotel {
@XmlElementWrapper(name = "RoomTypeVOs")
@XmlElement(name = "RoomTypeVO")
public List<RoomTypeVO> getRoomTypeVOs() {
return RoomTypeVOs;
}
public void setRoomTypeVOs(List<RoomTypeVO> roomTypeVOs) {
RoomTypeVOs = roomTypeVOs;
}
private List<RoomTypeVO> RoomTypeVOs;
/////@XmlElement(name = "id")
@XmlAttribute(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class RoomTypeVO {
///@XmlElement(name = "typeid")
@XmlAttribute(name = "typeid")
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
@XmlElement(name = "typename")
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
@XmlElement(name = "price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
private int typeid;
private String typename;
private String price;
}
}
jaxb简单工具类:
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;
/**
* 使用Jaxb2.0实现XML<->Java Object的Binder.
*
* 特别支持Root对象是List的情形.
*
* @author
*/
public class JaxbUtil {
// 多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types
* 所有需要序列化的Root对象的类型.
*/
public JaxbUtil(Class<?>... types) {
try {
jaxbContext = JAXBContext.newInstance(types);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml.
*/
public String toXml(Object root, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(root, writer);
return writer.toString();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml, 特别支持对Root Element是Collection的情形.
*/
@SuppressWarnings("unchecked")
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 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);
}
}
/**
* Xml->Java Object, 支持大小写敏感或不敏感.
*/
@SuppressWarnings("unchecked")
public <T> T fromXml(String xml, boolean caseSensitive) {
try {
String fromXml = xml;
if (!caseSensitive)
fromXml = xml.toLowerCase();
StringReader reader = new StringReader(fromXml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 创建Marshaller, 设定encoding(可为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 new RuntimeException(e);
}
}
/**
* 创建UnMarshaller.
*/
public Unmarshaller createUnmarshaller() {
try {
return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 封装Root Element 是 Collection的情况.
*/
public static class CollectionWrapper {
@SuppressWarnings("unchecked")
@XmlAnyElement
protected Collection collection;
}
}
输出如下:
xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<hotel id="1">
<name>name1</name>
<RoomTypeVOs>
<RoomTypeVO typeid="1">
<price>20</price>
<typename>typename1</typename>
</RoomTypeVO>
<RoomTypeVO typeid="2">
<price>30</price>
<typename>typename2</typename>
</RoomTypeVO>
</RoomTypeVOs>
</hotel>
hotelid:1
hotelname:name1
Typeid:1
Typename:typename1
Typeid:2
Typename:typename2
import java.util.List;
import com.yanek.test.JaxbUtil.CollectionWrapper;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//创建java对象
Hotel hotel=new Hotel();
hotel.setId(1);
hotel.setName("name1");
RoomTypeVO t1=new RoomTypeVO();
t1.setPrice("20");
t1.setTypeid(1);
t1.setTypename("typename1");
RoomTypeVO t2=new RoomTypeVO();
t2.setPrice("30");
t2.setTypeid(2);
t2.setTypename("typename2");
List<RoomTypeVO> RoomTypeVOs=new ArrayList<RoomTypeVO>();
RoomTypeVOs.add(t1);
RoomTypeVOs.add(t2);
hotel.setRoomTypeVOs(RoomTypeVOs);
//将java对象转换为XML字符串
JaxbUtil requestBinder = new JaxbUtil(Hotel.class,
CollectionWrapper.class);
String retXml = requestBinder.toXml(hotel, "utf-8");
System.out.println("xml:"+retXml);
//将xml字符串转换为java对象
JaxbUtil resultBinder = new JaxbUtil(Hotel.class,
CollectionWrapper.class);
Hotel hotelObj = resultBinder.fromXml(retXml);
System.out.println("hotelid:"+hotelObj.getId());
System.out.println("hotelname:"+hotelObj.getName());
for(RoomTypeVO roomTypeVO:hotelObj.getRoomTypeVOs())
{
System.out.println("Typeid:"+roomTypeVO.getTypeid());
System.out.println("Typename:"+roomTypeVO.getTypename());
}
}
实体类:import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "hotel")
public class Hotel {
@XmlElementWrapper(name = "RoomTypeVOs")
@XmlElement(name = "RoomTypeVO")
public List<RoomTypeVO> getRoomTypeVOs() {
return RoomTypeVOs;
}
public void setRoomTypeVOs(List<RoomTypeVO> roomTypeVOs) {
RoomTypeVOs = roomTypeVOs;
}
private List<RoomTypeVO> RoomTypeVOs;
/////@XmlElement(name = "id")
@XmlAttribute(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class RoomTypeVO {
///@XmlElement(name = "typeid")
@XmlAttribute(name = "typeid")
public int getTypeid() {
return typeid;
}
public void setTypeid(int typeid) {
this.typeid = typeid;
}
@XmlElement(name = "typename")
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
@XmlElement(name = "price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
private int typeid;
private String typename;
private String price;
}
}
jaxb简单工具类:
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;
/**
* 使用Jaxb2.0实现XML<->Java Object的Binder.
*
* 特别支持Root对象是List的情形.
*
* @author
*/
public class JaxbUtil {
// 多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types
* 所有需要序列化的Root对象的类型.
*/
public JaxbUtil(Class<?>... types) {
try {
jaxbContext = JAXBContext.newInstance(types);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml.
*/
public String toXml(Object root, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(root, writer);
return writer.toString();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Java Object->Xml, 特别支持对Root Element是Collection的情形.
*/
@SuppressWarnings("unchecked")
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 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);
}
}
/**
* Xml->Java Object, 支持大小写敏感或不敏感.
*/
@SuppressWarnings("unchecked")
public <T> T fromXml(String xml, boolean caseSensitive) {
try {
String fromXml = xml;
if (!caseSensitive)
fromXml = xml.toLowerCase();
StringReader reader = new StringReader(fromXml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 创建Marshaller, 设定encoding(可为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 new RuntimeException(e);
}
}
/**
* 创建UnMarshaller.
*/
public Unmarshaller createUnmarshaller() {
try {
return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 封装Root Element 是 Collection的情况.
*/
public static class CollectionWrapper {
@SuppressWarnings("unchecked")
@XmlAnyElement
protected Collection collection;
}
}
输出如下:
xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<hotel id="1">
<name>name1</name>
<RoomTypeVOs>
<RoomTypeVO typeid="1">
<price>20</price>
<typename>typename1</typename>
</RoomTypeVO>
<RoomTypeVO typeid="2">
<price>30</price>
<typename>typename2</typename>
</RoomTypeVO>
</RoomTypeVOs>
</hotel>
hotelid:1
hotelname:name1
Typeid:1
Typename:typename1
Typeid:2
Typename:typename2
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Python动态类型的学习---引用的理解
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序