Jaxb2 transform XML Document--Complete Java object and XML conversion __java

Source: Internet
Author: User
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 ();
            }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.