Jaxb implements mutual conversion between Java objects and XML

Source: Internet
Author: User

Marshaller and unmarshaller can be used to implement conversion between Java object and XML.

First, create a simple boy object.

[Java]
View plaincopyprint?
  1. @ Xmlrootelement (name = "root ")
  2. @ Xmlaccessortype (xmlaccesstype. Property)
  3. Public class boy {
  4. Private string name = "AA ";
  5. Public String getname (){
  6. Return name;
  7. }
  8. Public void setname (string name ){
  9. This. Name = Name;
  10. }
  11. }
@XmlRootElement(name="Root")@XmlAccessorType(XmlAccessType.PROPERTY)public class Boy {private String name = "aa";public String getName() {return name;}public void setName(String name) {this.name = name;}}

@ Xmlrootelement (name = "root") indicates the root element of the XML file. (name = "root") This is the element that redefines the XML file. If this name is not defined, the XML root element is consistent with the object name by default.

Then, the marshaller class is used to convert the object to XML, and the unmarshaller class can also be used to convert between XML and classes.

[Java]
View plaincopyprint?
  1. Public class jaxbtest {
  2. Public static void main (string [] ARGs) throws jaxbexception {
  3. Jaxbcontext context = jaxbcontext. newinstance (boy. Class );
  4. Extends aller extends aller = context. createmarshaller ();
  5. Unmarshaller = context. createunmarshaller ();
  6. System. Out. println ("---------- using aller --------------");
  7. Boy boy = new boy ();
  8. Using aller. Marshal (boy, system. Out );
  9. System. Out. println ("\ n ---------- unmarshaller --------------");
  10. // Convert XML to the corresponding Java object
  11. String xml = "<root> <Name> AA </Name> </root>"; // The tag name must be consistent with the property of the boy object.
  12. Boy B = (boy) unmarshaller. unmarshal (New stringreader (XML ));
  13. System. Out. println (B. getname ());
  14. }
  15. }
Public class jaxbtest {public static void main (string [] ARGs) throws jaxbexception {jaxbcontext context = jaxbcontext. newinstance (boy. class); extends aller = context. createmarshaller (); unmarshaller = context. createunmarshaller (); system. out. println ("---------- extends aller --------------"); boy = new boy (); extends aller. marshal (boy, system. out); system. out. println ("\ n ---------- unmarshaller --------------"); // convert XML to the corresponding Java object string xml = "<root> <Name> AA </Name> </root> "; // here the tag name must be the same as that of the boy object. Boy B = (boy) unmarshaller. unmarshal (New stringreader (XML); system. out. println (B. getname ());}}

Finally, convert and print the result.

---------- Marshaller --------------
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Root> <Name> AA </Name> </root>
---------- Unmarshaller --------------
AA

The object boy described above only contains simple attributes. What should I do if boy contains other objects?

[Java]
View plaincopyprint?
  1. @ Xmlrootelement (name = "root ")
  2. @ Xmlaccessortype (xmlaccesstype. Property)
  3. Public class boy {
  4. Private string name = "AA ";
  5. Private address;
  6. Public String getname (){
  7. Return name;
  8. }
  9. Public void setname (string name ){
  10. This. Name = Name;
  11. }
  12. @ Xmljavatypeadapter (addressadapter. Class)
  13. Public Address getaddress (){
  14. Return address;
  15. }
  16. Public void setaddress (Address ){
  17. This. Address = address;
  18. }
  19. }
@XmlRootElement(name="Root")@XmlAccessorType(XmlAccessType.PROPERTY)public class Boy {private String name = "aa";private Address address;public String getName() {return name;}public void setName(String name) {this.name = name;}@XmlJavaTypeAdapter(AddressAdapter.class)public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}}
[Java]
View plaincopyprint?
  1. Public Class address {
  2. Private string address;
  3. Public void setaddress (string address ){
  4. This. Address = address;
  5. }
  6. Public String getaddress (){
  7. Return address;
  8. }
  9. }
public class Address {private String address;public void setAddress(String address) {this.address = address;}public String getAddress() {return address;}}

In this case, the boy object contains the address object. Therefore, the getaddress () in the boy object must be set as an adapter,

Create addressadapter class, inherit xmladapter <valuetype, boundtype>

[Java]
View plaincopyprint?
  1. Public class addressadapter extends xmladapter <string, address> {
  2. @ Override
  3. Public Address unmarshal (string v) throws exception {
  4. Addressimpl address = new addressimpl ();
  5. Address. setaddress (v );
  6. Return address;
  7. }
  8. @ Override
  9. Public String Marshal (address v) throws exception {
  10. Return v. getaddress ();
  11. }
  12. }
public class AddressAdapter extends XmlAdapter<String, Address> {@Overridepublic Address unmarshal(String v) throws Exception {AddressImpl address = new AddressImpl();address.setAddress(v);return address;}@Overridepublic String marshal(Address v) throws Exception {return v.getAddress();}}

In this case, the Java object can be converted to XML.

[Java]
View plaincopyprint?
  1. Public class jaxbtest {
  2. Public static void main (string [] ARGs) throws jaxbexception {
  3. Jaxbcontext context = jaxbcontext. newinstance (boy. Class );
  4. Extends aller extends aller = context. createmarshaller ();
  5. System. Out. println ("---------- using aller --------------");
  6. Boy boy = new boy ();
  7. Addressimpl address = new addressimpl ();
  8. Address. setaddress ("Beijing ");
  9. Boy. setaddress (Address );
  10. Using aller. Marshal (boy, system. Out );
  11. }
  12. }
public class JAXBTest {public static void main(String[] args) throws JAXBException {JAXBContext context = JAXBContext.newInstance(Boy.class);Marshaller marshaller = context.createMarshaller();System.out.println("----------marshaller--------------");Boy boy = new Boy();AddressImpl address = new AddressImpl();address.setAddress("BeiJing");boy.setAddress(address);marshaller.marshal(boy, System.out);}}

The output is as follows:

<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Root> <address> Beijing </address> <Name> AA </Name> </root>

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.