Using JDK XML and Java objects to convert each other

Source: Internet
Author: User

The Unmarshaller class can convert XML data to Java content objects.

The Marshaller class is able to convert Java objects back to XML data.

 PackageJAXB;/*** Created by sheting on 10/17/2017*/ Public classClassroom {Private intID; PrivateString name; Private intgrade;  PublicClassroom () {} PublicClassroom (intID, String name,intgrade) {        Super();  This. ID =ID;  This. Name =name;  This. grade =grade; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetgrade () {returngrade; }     Public voidSetgrade (intgrade) {         This. grade =grade; }}
 PackageJAXB;Importjavax.xml.bind.annotation.XmlRootElement;/*** Created by sheting on 10/17/2017*/@XmlRootElement Public classStudent {Private intID; PrivateString name; Private intAge ; PrivateClassroom Classroom;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicClassroom Getclassroom () {returnClassroom; }     Public voidsetclassroom (Classroom classroom) { This. Classroom =Classroom; }     PublicStudent (intID, String name,intAge , 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.      PublicStudent () {Super(); }}
 PackageJAXB;Importorg.junit.Test;ImportJavax.xml.bind.JAXBContext;Importjavax.xml.bind.JAXBException;ImportJavax.xml.bind.Marshaller;ImportJavax.xml.bind.Unmarshaller;ImportJava.io.StringReader;/*** Created by sheting on 10/17/2017*/ Public classTESTJAXB {@Test Public voidBeantoxml () {Classroom Classroom=NewClassroom (1, "Software Engineering", 4); Student Student=NewStudent (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 voidXmlstringtobean () {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 (NewStringReader (XMLSTR));            System.out.println (Student.getage ());        System.out.println (Student.getclassroom (). GetName ()); } Catch(jaxbexception e) {e.printstacktrace (); }    }}

Attention:

1, need to convert the model object must be added @xmlrootelement annotations, the other objects inside it does not need

2. The model object that needs to be converted must have a constructor with no parameters, including the object referenced inside the object.

JAXB (Java Architecture for XML Binding) is an industry standard and a technology that can produce Java classes based on XML schemas. In this process, JAXB also provides a way to reverse-generate the Java object tree from the XML instance document, and can re-write the contents of the Java object tree to the XML instance document. On the other hand, JAXB provides a quick and easy way to bind XML schemas to Java representations, making it easy for Java developers to combine XML data and processing functions in Java applications.

The Jaxbcontext class provides a client entry point to the JAXB API. It provides an abstraction to manage the Xml/java binding information required to implement the JAXB binding framework operations, including: Ungroup, group, and validate.

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

Using JDK XML and Java objects to convert 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.