——完成Java对象和XML相互转换
前面有介绍过json-lib这个框架,在线博文:
http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html
以及Jackson这个框架,在线博文: http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
它们都可以完成Java对象到XML的转换,但是还不是那么的完善。
还有XStream对JSON及XML的支持,它可以对JSON或XML的完美转换。在线博文:
http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
以及介绍Castor来完成Java对象到xml的相互转换。在线博文: http://www.cnblogs.com/hoojo/archive/2011/04/25/2026819.html
这次介绍Jaxb2完成xml的转换,Jaxb2使用了JDK的新特性。如:Annotation、GenericType等,Jaxb2需要在即将转换的JavaBean中添加annotation注解。下面我们就来看看Jaxb2是怎么样完成Java对象到XML之间的相互转换吧。
原文地址
Jaxb2 转换XML文档
http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html
以下是学习笔记:
1,改造成Maven工程后的pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coderdream</groupId>
<artifactId>jaxb2-demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jaxb2-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<build>
<finalName>jaxb2-demo</finalName>
</build>
</project>
2,完整的Jaxb2Test.java文件:
package com.hoo.test;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hoo.entity.Account;
import com.hoo.entity.AccountBean;
import com.hoo.entity.Birthday;
import com.hoo.entity.ListBean;
import com.hoo.entity.MapBean;
/**
* function:Jaxb2 完成Java和XML的编组、解组
*
* @author hoojo
* @createDate 2011-4-25 上午11:54:06
* @file Jaxb2Test.java
* @package com.hoo.test
* @project WebHttpUtils
* @blog http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
public class Jaxb2Test {
private JAXBContext context = null;
private StringWriter writer = null;
private StringReader reader = null;
private AccountBean bean = null;
@Before
public void init() {
bean = new AccountBean();
bean.setAddress("北京");
bean.setEmail("email");
bean.setId(1);
bean.setName("jack");
Birthday day = new Birthday();
day.setBirthday("2010-11-22");
bean.setBirthday(day);
try {
context = JAXBContext.newInstance(AccountBean.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void destory() {
context = null;
bean = null;
try {
if (writer != null) {
writer.flush();
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.gc();
}
public void fail(Object o) {
System.out.println(o);
}
public void failRed(Object o) {
System.err.println(o);
}
@Test
public void testBean2XML() {
try {
failRed("---------testBean2XML---------");
// 下面代码演示将对象转变为xml
Marshaller mar = context.createMarshaller();
writer = new StringWriter();
mar.marshal(bean, writer);
fail(writer);
// 下面代码演示将上面生成的xml转换为对象
reader = new StringReader(writer.toString());
Unmarshaller unmar = context.createUnmarshaller();
bean = (AccountBean) unmar.unmarshal(reader);
fail(bean);
} catch (JAXBException e) {
e.printStackTrace();
}
}
@Test
public void testList2XML() {
failRed("---------testList2XML---------");
ListBean listBean = new ListBean();
listBean.setName("list to xml");
List<Object> list = new ArrayList<Object>();
list.add(bean);
bean = new AccountBean();
bean.setAddress("china");
bean.setEmail("[email protected]");
bean.setId(2);
bean.setName("tom");
Birthday day = new Birthday("2010-11-22");
bean.setBirthday(day);
Account acc = new Account();
acc.setAddress("china");
acc.setEmail("[email protected]");
acc.setId(2);
acc.setName("tom");
day = new Birthday("2010-11-22");
acc.setBirthday(day);
list.add(bean);
list.add(acc);
listBean.setList(list);
try {
context = JAXBContext.newInstance(ListBean.class);
// 下面代码演示将对象转变为xml
Marshaller mar = context.createMarshaller();
writer = new StringWriter();
mar.marshal(listBean, writer);
fail(writer);
// 下面代码演示将上面生成的xml转换为对象
reader = new StringReader(writer.toString());
Unmarshaller unmar = context.createUnmarshaller();
listBean = (ListBean) unmar.unmarshal(reader);
fail(listBean.getList().get(0));
fail(listBean.getList().get(1));
fail(listBean.getList().get(2));
} catch (JAXBException e) {
e.printStackTrace();
}
}
/**
* <b>function:</b>Map需要自己实现,可以构造一个简单的Map对象实现
* http://metro.1045641.n5.nabble.com/Does-JAXB-2-0-support-Map-td1058084.html
*
* @author hoojo
* @createDate 2010-12-1 上午10:23:26
*/
@Test
public void testMap2XML() {
failRed("---------testMap2XML---------");
MapBean mapBean = new MapBean();
HashMap<String, AccountBean> map = new HashMap<String, AccountBean>();
map.put("NO1", bean);
bean = new AccountBean();
bean.setAddress("china");
bean.setEmail("[email protected]");
bean.setId(2);
bean.setName("tom");
Birthday day = new Birthday("2010-11-22");
bean.setBirthday(day);
map.put("NO2", bean);
mapBean.setMap(map);
try {
context = JAXBContext.newInstance(MapBean.class);
// 下面代码演示将对象转变为xml
Marshaller mar = context.createMarshaller();
writer = new StringWriter();
mar.marshal(mapBean, writer);
fail(writer);
// 下面代码演示将上面生成的xml转换为对象
reader = new StringReader(writer.toString());
Unmarshaller unmar = context.createUnmarshaller();
mapBean = (MapBean) unmar.unmarshal(reader);
fail(mapBean.getMap());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
3,运行结果:
---------testMap2XML---------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><mapBean><map><item><key>NO2</key><value number="2"><address>china</address><birthday><birthday>2010-11-22</birthday></birthday><email>[email protected]</email><name>tom</name></value></item><item><key>NO1</key><value number="1"><address>北京</address><birthday><birthday>2010-11-22</birthday></birthday><email>email</email><name>jack</name></value></item></map></mapBean>
{NO2=tom#2#china#2010-11-22#[email protected], NO1=jack#1#北京#2010-11-22#email}
---------testBean2XML---------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><account number="1"><address>北京</address><birthday><birthday>2010-11-22</birthday></birthday><email>email</email><name>jack</name></account>
jack#1#北京#2010-11-22#email
---------testList2XML---------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><list-bean name="list to xml"><bean number="1"><address>北京</address><birthday><birthday>2010-11-22</birthday></birthday><email>email</email><name>jack</name></bean><bean number="2"><address>china</address><birthday><birthday>2010-11-22</birthday></birthday><email>[email protected]</email><name>tom</name></bean><account><address>china</address><birthday><birthday>2010-11-22</birthday></birthday><email>[email protected]</email><id>2</id><name>tom</name></account></list-bean>
jack#1#北京#2010-11-22#email
tom#2#china#2010-11-22#[email protected]
2#tom#[email protected]#china#2010-11-22
4,git源代码地址:
https://git.oschina.net/coderdream/jaxb2-demo
5,源代码下载: