The JDK comes with XML and Java objects converting to each other

Source: Internet
Author: User

the following is a class that comes with the JDK and does not reference any third-party jar packages .

The Unmarshaller class enables client applications to convert XML data to a Java content object tree.

Remark: Marshal (serialization, arrangement, sorting)

The Marshaller class enables client applications to convert the Java content tree back to XML data.

PACKAGE HB.JAXB;  IMPORT JAVAX.XML.BIND.ANNOTATION.XMLROOTELEMENT;  //1, The model object that needs to be converted must add @xmlrootelement annotations, and other objects within it (classroom) do not need to be @xmlrootelement  public class  student {      private int id;       private String name;      private int age;       private Classroom classroom;      public  Int getid ()  {          return id;       }      public void setid (Int id)  {           this.id = id;       }      public string getname ()  {         &nbSp; return name;      }      public  void setname (String name)  {           This.name = name;      }      public  int getage ()  {          return age;       }      public void setage (int  age)  {          this.age = age;       }      public classroom getclassroom ()  {          return classroom;       }      public void setclassroom (Classroom  Classroom)  {          this.classroom = classroom;      }       public student (Int id, string name, int age,  classroom classroom)  {          super ();           this.id = id;           this.name = name;           this.age = age;           this.classroom = classroom;      }       No argument enough for the function must be required, otherwise the Jxbcontext cannot parse normally.       public student ()  {           super ();       }  }


PACKAGE HB.JAXB;  //2, the model object that needs to be converted must have a constructor with no arguments, including the object referenced inside the object. public class classroom {      private int id;       private String name;      private  Int grade;      public int getid ()  {           return id;      }       public void setid (Int id)  {           this.id = id;      }       public string getname ()  {           Return name;      }      public void  setname (String name)  {          this.name = name;      }       public int getgrade ()  {           return grade;      }      public  void setgrade (Int grade)  {           This.grade = grade;      }      public  classroom (Int id, string name, int grade)  {           super ();           this.id  = id;          this.name = name;           this.grade = grade;       }  &nbSp;   public classroom ()  {           super ();       }  }



package hb.jaxb;  import java.io.stringreader;  import  javax.xml.bind.jaxbcontext;  import javax.xml.bind.jaxbexception;  import  javax.xml.bind.marshaller;  import javax.xml.bind.unmarshaller;  import  org.junit.test;  //eclipse console //Chinese garbled solution  eclipse-->Run-->Run  configuration-->common--> console encoding-->others-->utf-8//Reference Blog:/http hbiao68.iteye.com/blog/1958413public class testjaxb {       @Test       public void beantoxml ()  {           classroom classroom = new classroom (1,  "Software Engineering", &NBSP;4);           student student = new student ( 101,  "Zhang San",  22, classroom);           try {               jaxbcontext context = jaxbcontext.newinstance (Student.class);               Marshaller marshaller =  Context.createmarshaller ();               marshaller.marshal (student, system.out);           } catch  (jaxbexception e)  {               e.printstacktrace ();           }      }             @Test       public void xmlstringtobean () {           string xmlstr =  "<?xml version=\" 1.0\ " encoding=\" utf-8\ " standalone=\" yes\ "? ><student><age>22</age><classroom><grade>4</grade><id>1</id ><name> Software engineering </name></classroom><id>101</id><name> Zhang San </name></ Student> ";          try {               JAXBContext context =  Jaxbcontext.newinstance (Student.class);               unmarshaller unmarshaller = context.createunmarshaller ();               Student student =  (Student) Unmarshaller.unmarshal (New stringreader (XMLSTR));               system.out.prinTLN (Student.getage ());               System.out.println (Student.getclassroom (). GetName ());           } catch  (jaxbexception e)  {               e.printstacktrace ();           }                }   }

Reference blog:

http://hbiao68.iteye.com/blog/1958413


This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1759334

The JDK comes with XML and Java objects converting to each other

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.