JAXB implements the transformation of complex pojo and XML

Source: Internet
Author: User
Tags bind dateformat

Jaxml each parameter see: http://blog.csdn.net/z69183787/article/details/12970523

Person:

Package com.test;
Import Java.util.Date;

Import java.util.List;
Import javax.xml.bind.annotation.XmlElement;
Import javax.xml.bind.annotation.XmlRootElement;
Import javax.xml.bind.annotation.XmlTransient;

Import Javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
	
	@XmlRootElement (name = ' person ') public class Person {@XmlElement (name = "name") private String name;
	
	@XmlElement (name = ' age ') private String age;
	
	
	@XmlElement (name = "Basicdata") private list<child> children;
	
	Private Date D;
	@XmlElement (name = "Time") @XmlJavaTypeAdapter (jaxbdateserializer.class) public Date getd () {return D;
	public void setd (Date d) {this.d = D;
	} @XmlTransient Public list<child> GetChildren () {return children;
	} public void Setchildren (list<child> children) {This.children = children;
	} @XmlTransient Public String getName () {return name;
	} public void SetName (String name) {this.name = name; } @XmlTransient public String getAge () {return age;
	public void Setage (String age) {this.age = age;
 }
	
}


Child.java

Package com.test;

Import Javax.xml.bind.annotation.XmlAccessorType;
Import javax.xml.bind.annotation.XmlRootElement;

Import Javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement (name= "Basicdata")
@XmlAccessorType (Xmlaccesstype.field) Public
class Child {
	private String sex;

	Public String Getsex () {
		return sex;
	}

	public void Setsex (String sex) {
		this.sex = sex;
	}
	
	
}


Jaxbdateserializer.java Processing Date

Package com.test;

Import Java.text.SimpleDateFormat;
Import java.util.Date;

Import Javax.xml.bind.annotation.adapters.XmlAdapter;

public class Jaxbdateserializer extends Xmladapter<string, date>{
    private SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
    @Override public 
    String Marshal (date date) throws Exception { 
        return Dateformat.format (date); 
    }
    @Override Public 
    Date Unmarshal (String date) throws Exception { 
        return dateformat.parse (Date); 
    } 
}


Main

Package com.test;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.StringWriter;
Import Java.net.URL;
Import Java.util.Date;
Import java.util.ArrayList;

Import java.util.List;
Import Javax.xml.bind.JAXBContext;
Import javax.xml.bind.JAXBException;
Import Javax.xml.bind.Marshaller;


Import Javax.xml.bind.Unmarshaller; public class Test {public static void main (string[] args) throws Jaxbexception, ioexception{System.out.println (JAVA.N Et.
		Urlencoder.encode ("The", "Utf-8"));
		Java class to XML file list<child> L = new arraylist<child> ();
		Person p = new person ();
		P.setage ("123");
		P.setname ("Epilator");
		P.SETD (New Date ());
			for (int. i=0;i<5;i++) {child-c = new Child ();
			C.setsex (i+ "");
		L.add (c);
		} p.setchildren (L);
		StringWriter writer = new StringWriter ();
		URL read = Classloader.getsystemresource ("Com/test/person.xml");
		FileWriter FW = new FileWriter (Read.getpath ());
		Jaxbcontext context = jaxbcontext.newinstance (Person.class); MarShaller m = Context.createmarshaller (); 
		M.setproperty (Marshaller.jaxb_formatted_output, true); M.setproperty (marshaller.jaxb_encoding, "GBK");
		Prevent file Chinese garbled M.marshal (p, writer);
		M.marshal (P, FW);
		Fw.flush ();
		Fw.close ();
		String XML = writer.tostring ();
		
		
	SYSTEM.OUT.PRINTLN (XML);
	        XML file to Java class Jaxbcontext context2 = Jaxbcontext.newinstance (Person.class);
	        Unmarshaller Unmarshaller = Context2.createunmarshaller ();
	        Class Directory Jaxb/bin//url xmlfileurl = Classloader.getsystemresource ("");
	        URL Xmlfileurl = Classloader.getsystemresource ("Com/test/person.xml");
	        System.out.println (Xmlfileurl.getpath ());
               person P2 = (person) unmarshaller.unmarshal (Xmlfileurl);  saxreader reader = new Saxreader ();             //  Document document = Reader.read (new File (      &N Bsp    //test.class.getclassloader (). GetResource ("Com/test/person.xml").GetPath ()));
	             // person p2 = (person) Unmarshaller.unmarshal (new StringReader (Document.asxml ()));
	        System.out.println (P2.getname ());
	System.out.println (P2.getchildren (). Get (4). Getsex ());
 }
}



Xml

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<person><name>zhoushun</name> <age>123</age><basicdata><sex>0</sex></basicdata><basicdata><sex >1</sex></basicdata><basicdata><sex>2</sex></basicdata><basicdata ><sex>3</sex></BasicData><BasicData><sex>4</sex></BasicData>< Time>2013-10-31</time>
</person>


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.