Marshaller and unmarshaller can be used to implement conversion between Java object and XML.
First, create a simple boy object.
[Java]
View plaincopyprint?
- @ 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")@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?
- Public class jaxbtest {
- Public static void main (string [] ARGs) throws jaxbexception {
- Jaxbcontext context = jaxbcontext. newinstance (boy. Class );
- Extends aller extends aller = context. createmarshaller ();
- Unmarshaller = context. createunmarshaller ();
- System. Out. println ("---------- using aller --------------");
- Boy boy = new boy ();
- Using aller. Marshal (boy, system. Out );
- System. Out. println ("\ n ---------- unmarshaller --------------");
- // Convert XML to the corresponding Java object
- String xml = "<root> <Name> AA </Name> </root>"; // The tag name must be consistent with the property of the boy object.
- Boy B = (boy) unmarshaller. unmarshal (New stringreader (XML ));
- System. Out. println (B. getname ());
- }
- }
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?
- @ Xmlrootelement (name = "root ")
- @ Xmlaccessortype (xmlaccesstype. Property)
- Public class boy {
- Private string name = "AA ";
- Private 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 ){
- This. Address = address;
- }
- }
@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?
- Public Class address {
- Private string address;
- Public void setaddress (string address ){
- This. Address = address;
- }
- Public String getaddress (){
- Return address;
- }
- }
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?
- Public class addressadapter extends xmladapter <string, address> {
- @ Override
- Public Address unmarshal (string v) throws exception {
- Addressimpl address = new addressimpl ();
- Address. setaddress (v );
- Return address;
- }
- @ Override
- Public String Marshal (address v) throws exception {
- Return v. getaddress ();
- }
- }
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?
- Public class jaxbtest {
- Public static void main (string [] ARGs) throws jaxbexception {
- Jaxbcontext context = jaxbcontext. newinstance (boy. Class );
- Extends aller extends aller = context. createmarshaller ();
- System. Out. println ("---------- using aller --------------");
- Boy boy = new boy ();
- Addressimpl address = new addressimpl ();
- Address. setaddress ("Beijing ");
- Boy. setaddress (Address );
- Using aller. Marshal (boy, system. Out );
- }
- }
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>