Jaxb2 Transform XML Document--Complete Java object and XML conversion
This time, Jaxb2 completes the conversion of XML, JAXB2 uses the new features of JDK. such as: Annotation, GenericType and so on, Jaxb2 need to add Annotation annotation in the JavaBean that will be converted. Let's take a look at how Jaxb2 is completing the conversion of Java objects to XML.
First, preparatory work
1. Resource Preparation
A) official documents: http://www.oracle.com/technetwork/articles/javase/index-140168.html
b) Jar pack Download: Http://jaxb.java.net/2.2.3/JAXB2_20101209.jar
If you have a jar to add JDK to the project, there is a jaxb2 in the Rt.jar. In general, you do not need to add jaxb2 jars yourself.
2, code preparation before the program
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 Java.util.Map;
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 Complete the Java and XML grouping, solution Group
* @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 hoojo_@126.com
* @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 ("Beijing");
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 ();
}