JAXB Hello World

Source: Internet
Author: User

JAXB is short for Java Architecture for XML Binding. Use the JAXB annotation to convert a Java object to an XML file. In this tutorial, we will show you how to use JAXB to do the following:

Marshalling-converts a Java object to an XML file.
Unmarshalling-converts XML content into a Java object.
Related Technologies used in this article:

JDK 1.6
JAXB 2.0
It is easy to use JAXB. You only need to annotate the Object with JAXB annotations, and then use jaxbMarshaller. marshal () or jaxbMarshaller. unmarshal () for XML/Object conversion.

1. JAXB dependency
If you are using JDK 1.6 or later, you do not need to add additional class libraries because JAXB is bound to JDK 1.6.

Note:

If you are using JDK <1.6, you need to add the downloaded "jaxb-api.jar" and "jaxb-impl.jar" packages to your project CLASSPATH.

2. JAXB Annotation (Annotation)
If an object needs to be converted to an XML file or generated from the XML file, the object needs to be annotated with JAXB annotations. These annotations simply show what they mean by name. For details, refer to the official website: jaxb guide

[Java]
Package com. jaxb. core;

Import javax. xml. bind. annotation. XmlAttribute;
Import javax. xml. bind. annotation. XmlElement;
Import javax. xml. bind. annotation. XmlRootElement;

@ XmlRootElement
Public class Customer {

String name;
Int age;
Int id;

Public String getName (){
Return name;
}

@ XmlElement
Public void setName (String name ){
This. name = name;
}

Public int getAge (){
Return age;
}

@ XmlElement
Public void setAge (int age ){
This. age = age;
}

Public int getId (){
Return id;
}

@ XmlAttribute
Public void setId (int id ){
This. id = id;
}

}

Package com. jaxb. core;
 
Import javax. xml. bind. annotation. XmlAttribute;
Import javax. xml. bind. annotation. XmlElement;
Import javax. xml. bind. annotation. XmlRootElement;
 
@ XmlRootElement
Public class Customer {
 
String name;
Int age;
Int id;
 
Public String getName (){
Return name;
}
 
@ XmlElement
Public void setName (String name ){
This. name = name;
}
 
Public int getAge (){
Return age;
}
 
@ XmlElement
Public void setAge (int age ){
This. age = age;
}
 
Public int getId (){
Return id;
}
 
@ XmlAttribute
Public void setId (int id ){
This. id = id;
}
 
}
3. convert an object to XML
In the JAXB marshalling example, the customer object is converted into an XML file. JaxbMarshaller. marshal () contains many overload methods, so you can choose the method if the output meets your requirements.

[Java]
Package com. jaxb. core;

Import java. io. File;
Import javax. xml. bind. JAXBContext;
Import javax. xml. bind. JAXBException;
Import javax. xml. bind. Marshaller;

Public class JAXBExample {
Public static void main (String [] args ){

Customer customer = new Customer ();
Customer. setId (100 );
Customer. setName ("benson ");
Customer. setAge (23 );

Try {

File file = new File ("C: \ file. xml ");
JAXBContext jaxbContext = JAXBContext. newInstance (Customer. class );
Marshaller jaxbMarshaller = jaxbContext. createMarshaller ();

// Output pretty printed
JaxbMarshaller. setProperty (Marshaller. JAXB_FORMATTED_OUTPUT, true );

JaxbMarshaller. marshal (customer, file );
JaxbMarshaller. marshal (customer, System. out );

} Catch (JAXBException e ){
E. printStackTrace ();
}

}
}

Package com. jaxb. core;
 
Import java. io. File;
Import javax. xml. bind. JAXBContext;
Import javax. xml. bind. JAXBException;
Import javax. xml. bind. Marshaller;
 
Public class JAXBExample {
Public static void main (String [] args ){
 
Customer customer = new Customer ();
Customer. setId (100 );
Customer. setName ("benson ");
Customer. setAge (23 );
 
Try {
 
File file = new File ("C: \ file. xml ");
JAXBContext jaxbContext = JAXBContext. newInstance (Customer. class );
Marshaller jaxbMarshaller = jaxbContext. createMarshaller ();
 
// Output pretty printed
JaxbMarshaller. setProperty (Marshaller. JAXB_FORMATTED_OUTPUT, true );
 
JaxbMarshaller. marshal (customer, file );
JaxbMarshaller. marshal (customer, System. out );
 
} Catch (JAXBException e ){
E. printStackTrace ();
}
 
}
}

Output:
[Html]
<? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Customer id = "100" type = "apiname" text = "apiname">
<Age> 23 </age>
<Name> benson </name>
</Customer>

<? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Customer id = "100" type = "apiname" text = "apiname">
<Age> 23 </age>
<Name> benson </name>
</Customer>
 

4. Convert XML into an object:
In the JAXB unmarshalling example, the content of the XML file is converted into a customer object. JaxbMarshaller. unmarshal () contains many overload methods. You can choose the method that suits your output.

[Java]
Package com. jaxb. core;

Import java. io. File;
Import javax. xml. bind. JAXBContext;
Import javax. xml. bind. JAXBException;
Import javax. xml. bind. Unmarshaller;

Public class JAXBExample {
Public static void main (String [] args ){

Try {

File file = new File ("C: \ file. xml ");
JAXBContext jaxbContext = JAXBContext. newInstance (Customer. class );

Unmarshaller jaxbUnmarshaller = jaxbContext. createUnmarshaller ();
Customer customer = (Customer) jaxbUnmarshaller. unmarshal (file );
System. out. println (customer );

} Catch (JAXBException e ){
E. printStackTrace ();
}

}
}

Package com. jaxb. core;
 
Import java. io. File;
Import javax. xml. bind. JAXBContext;
Import javax. xml. bind. JAXBException;
Import javax. xml. bind. Unmarshaller;
 
Public class JAXBExample {
Public static void main (String [] args ){
 
Try {
 
File file = new File ("C: \ file. xml ");
JAXBContext jaxbContext = JAXBContext. newInstance (Customer. class );
 
Unmarshaller jaxbUnmarshaller = jaxbContext. createUnmarshaller ();
Customer customer = (Customer) jaxbUnmarshaller. unmarshal (file );
System. out. println (customer );
 
} Catch (JAXBException e ){
E. printStackTrace ();
}
 
}
}
Output:
[Plain]
Customer [name = benson, age = 23, id = 100]

Customer [name = benson, age = 23, id = 100]

 

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.