Using JAXB to generate Java classes through XSD, Java objects are converted to XML files

Source: Internet
Author: User
Tags object object

Come, nonsense not much to say, directly into the state.


First use Altova XMLSpy to draw an XSD file, or use the code to write an XSD file, directly write the code to check the data.

Just use the simplest student. The student.xsd of the painting

The code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <!--edited with XMLSpy v2011 (http://www.altova.com) by AA (AA)--> &L T;xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema" elementformdefault= "qualified" Unqualified "> <xs:element name=" Student "> <xs:annotation> <xs:documentation> student </xs: documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name= "id"
					; <xs:simpleType> <xs:restriction base= "Xs:int" > <xs:mininclusive value= "0"/> <xs:m Axinclusive value= "9999"/> </xs:restriction> </xs:simpleType> </xs:element> <xs : element name= "name" > <xs:simpleType> <xs:restriction base= "xs:string" > <xs:minlength value= "0"/> <xs:maxlength value= "/> </xs:restriction> </xs:simpleType> </ Xs:element> <xs:element name="Age" > <xs:simpleType> <xs:restriction base= "Xs:int" > <xs:mininclusive value= "0"/> <xs:maxinclusive value= "/>" </xs:restriction> </xs:simpleType> </xs:element&
				Gt <xs:element name= "Sex" > <xs:simpleType> <xs:restriction base= "xs:string" > <xs:minle
				Ngth value= "0"/> <xs:maxlength value= "ten"/> </xs:restriction> </xs:simpleType>
 </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

The tools I use here are: jaxb-ri-20120516;

Run DOS, and jump to C:\Users\BoiseWu\Desktop\jaxb-ri-20120516\bin (this is my path) under the path,

And put the generated student.xsd files under C:\Users\BoiseWu\Desktop\jaxb-ri-20120516\bin,

Set the output path of the Java class, run set output_path=d:\student d:\student this path must exist.

Implementation: xjc-d%output_path%-P com.boise.bean-b binding.xml student.xsd

In this Java class is generated.


The following are translated from Java objects into XML files and from XML files to Java objects

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.StringReader;

Import Java.io.StringWriter;
Import Javax.xml.bind.JAXBContext;
Import Javax.xml.bind.Marshaller;

Import Javax.xml.bind.Unmarshaller;
		public class ToXML {public static void main (string[] args) {Student Student = new Student ();
		Student.setid (1111);
		Student.setname ("Zhangsan");
		Student.setage (20);

		Student.setsex ("male");

		String xmlstr = Createxml (Student.class, Student);
		System.out.println (XMLSTR);

		Convert from Java object to XML byte[] Bytexml = Xmlstr.getbytes ();
			try {outputstream OS = new FileOutputStream (New File ("D:/student.xml"));
			Os.write (Bytexml);
			Os.flush ();
		Os.close ();
		catch (FileNotFoundException e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace (); ///translated from XML to Java object try{inputstReam is = new FileInputStream (New File ("D:/student.xml"));
			byte [] bytes = new BYTE[100];
			Xmlstr = "";
			int temp;
			while ((Temp=is.read (bytes))!=-1) {xmlstr +=new String (bytes,0,temp);
			
			} System.out.println (XMLSTR);
			Student stu = (Student) CreateObject (Student.class, XMLSTR);
		System.out.println (Stu.getname ());
		}catch (Exception e) {e.printstacktrace (); } public static <T> String createxml (class<t> entityclass, Object entity) {StringWriter writer = n
		EW StringWriter ();
			try {Jaxbcontext act = jaxbcontext.newinstance (entityclass);
			Marshaller Marshaller = Act.createmarshaller ();
			Marshaller.setproperty (Marshaller.jaxb_formatted_output, true);
			Marshaller.setproperty (marshaller.jaxb_encoding, "UTF-8");
		Marshaller.marshal (entity, writer);
		catch (Exception e) {e.printstacktrace ();
	return writer.tostring (); public static <T> Object CreateObject (class<t> Entityclass, String entity) {ObjeCT object = null;
			try {Jaxbcontext act = jaxbcontext.newinstance (entityclass);
			Unmarshaller Unmarshaller = Act.createunmarshaller ();
		Object = Unmarshaller.unmarshal (new StringReader (entity));
		catch (Exception e) {e.printstacktrace ();
	return object;
 }
}

Haha, this is OK.

The following are the output results:






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.