Conversion of Java object to XML

Source: Internet
Author: User

Overview: Before the transformation of XML and Java objects to understand less, today Learning interface API just contact, so write down, beginners hope everyone forgive ha.

1. Since it is a Java object and XML conversion, it is necessary to have a Java class to obtain the object, this example is mainly related to the boy class and the test run class

2. Code

2.1 Starting the first step of simple learning

@XmlRootElement

@XmlAccessorType (Xmlaccesstype.field)//field is just a field on a class, not a property

public class Boy {

String name = "xu**";

}

Public class Testxml {

public static void Main (String args[]) {

try {

Jaxbcontext Jaxbcontext = jaxbcontext.newinstance (Boy.class);

Marshaller Marshaller = Jaxbcontext.createmarshaller (); Convert an object to an XML format

Unmarshaller Unmarshaller = Jaxbcontext.createunmarshaller (); converting XML to Objects

Boy boy = new Boy ();

Marshaller.marshal (boy,system.out); //display in print output form

System.out.println ();

String xmlstr = "<boy><name> xu **</name></boy>";

Boy Towboy = (boy) Unmarshaller.unmarshal (new StringReader (XMLSTR));

System.out.println (Towboy.name);

} catch (Jaxbexception e) {

E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.

}

}

}

The result of the printing is:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy><name>xu**</name ></boy>

Xu * *

2.2 When boy turns down like this

@XmlRootElement

@XmlAccessorType (xmlaccesstype.property)//property is a property

Public class Boy {

String name = "xu**";

}

The result of the printing is:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy/>

xu**

2.3 If you want to have 2.1 results, you need to provide a Get method of name to get the property

@XmlRootElement

@XmlAccessorType (Xmlaccesstype.property)

Public class Boy {

String name = "123";

Public String GetName () {

return name;

    }

Public void SetName (String name) {

this.name = name;

    }

}

2.4 can also be annotated by XmlElement

@XmlRootElement

@XmlAccessorType (Xmlaccesstype.property)

public class Boy {

String name = "123";

@XmlElement

Integer age = 10;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

}

The result of the printing is:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy><age>10</age><name> 123</name></boy>

Xu * *

2.5 You can also change boy to tag and put it under a namespace

@XmlRootElement (name = "xu", namespace = "Http://test")

@XmlAccessorType (Xmlaccesstype.property)

Public class Boy {

@XmlElement

String name = "123";

@XmlElement

Integer age = ten;


}

Public class Testxml {

Public static void Main (String args[]) {

try {

Jaxbcontext jaxbcontext = jaxbcontext.newinstance (boy.class);

Marshaller Marshaller = Jaxbcontext.createmarshaller ();//Convert object to XML format

Unmarshaller Unmarshaller = Jaxbcontext.createunmarshaller ();//Convert XML to Object

Boy Boy = new Boy ();

Marshaller.marshal (boy,system.out);

System.out.println ();

String xmlstr = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "standalone=\" yes\ "? ><ns2:Xu Xmlns:ns2=\ "http://test\" ><name> Xu **</name><age>25</age></ns2:xu> ";

Boy Towboy = (boy) Unmarshaller.unmarshal (new StringReader (XMLSTR));

System.out.println (towboy.name);

System.out.println (towboy.age);

} catch (Jaxbexception e) {

E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.

          }

      }

}

The result of the printing is:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><ns2:Xu xmlns:ns2= "Http://test" > <name>123</name><age>10</age></ns2:xu>

Xu * *

-


2.6 @XmlJavaTypeAdaptor

@XmlRootElement (name = "Xu", namespace = "Http://test")

@XmlAccessorType (Xmlaccesstype.property)

Public class Boy {

@XmlElement

String name = "Xu * *";

@XmlElement

Integer age =;

private Testxmlinterface testxmlinterface;

}

We're going to write one more class. testxmlinterfaceadaptor is used to return a specific implementation class of testxmlinterface to the push

Interface Testxmlinterface cannot be converted when converting to XML, we have to add @XmlJavaTypeAdaptor (testxmlinterfaceadaptor.class)


This article is from the "Small blog" blog, be sure to keep this source http://9686567.blog.51cto.com/9676567/1587832

Conversion of Java object to XML

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.