0% found this document useful (0 votes)
19 views17 pages

3 Jaxb

JAXB (Java Architecture for XML Binding) is used to convert Java objects to XML and vice versa. It provides annotations to map Java classes to XML elements. The key steps to convert an object to XML are: generate classes from an XML schema, create a JAXBContext object, create a Marshaller to marshal the Java object to XML, and write the XML output to a file. To convert XML to an object, the XML file is unmarshalled using JAXBContext and Unmarshaller to read the data into a Java object.

Uploaded by

Muskan agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

3 Jaxb

JAXB (Java Architecture for XML Binding) is used to convert Java objects to XML and vice versa. It provides annotations to map Java classes to XML elements. The key steps to convert an object to XML are: generate classes from an XML schema, create a JAXBContext object, create a Marshaller to marshal the Java object to XML, and write the XML output to a file. To convert XML to an object, the XML file is unmarshalled using JAXBContext and Unmarshaller to read the data into a Java object.

Uploaded by

Muskan agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

JAXB

JAXB
• JAXB stands for Java Architecture for XML
Binding.
• It provides mechanism to marshal (write) java
objects into XML and unmarshal (read) XML
into object.
• Simply, you can say it is used to convert java
object into xml and vice-versa..
JAXB
Features of JAXB
• Annotation support: JAXB provides support to
annotation so less coding is required to
develop JAXB application.
• Support for all W3C XML Schema features
• Additional Validation Capabilities
• Small Runtime Library
Converting Object into XML
• The steps to convert an object to XML are as
follows:
– Bind the schema and generate the classes
– Create the JAXBContext object
– Create the Marshaller objects
– Create the content tree by using set methods
– Call the marshal method
Employee.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
int id;
String name;
float salary;
Employee.java (Contd)
public Employee() {}
public Employee(int id, String name, float salary)
{
super();
this.id = id;
this.name = name;
this.salary = salary;
}
Employee.java (Contd)
@XmlAttribute
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
Employee.java (Contd)
@XmlElement
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
Employee.java (Contd)
@XmlElement
public float getSalary()
{
return salary;
}
public void setSalary(float salary)
{
this.salary = salary;
}
} //End of class
ObjectToXml.java
import java.io.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class ObjectToXml
{
int id;
String name;
float sal;
ObjectToXml.java (Contd)
public static void main(String[] args) throws Exception
{
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter name”);
name= br.readLine();
System.out.println(“Enter salary”);
sal= Float.parseFloat(br.readLine());
System.out.println(“Enter ID”);
id= Integer.parseInt(br.readLine());
ObjectToXml.java (Contd)
JAXBContext obj= JAXBContext.newInstance(Employee.class);
Marshaller m = obj.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Employee emp1=new Employee(id, name, sal);


m.marshal(emp1, new FileOutputStream("emp.xml"));
} //End of main method
} //End of class
emp.xml (produced)
The generated xml file will look like this:

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


<employee id="1">
<name>Abc</name>
<salary>50000.0</salary>
</employee>
XMLToObject.java
import java.io.File;
import javax.xml.bind.*;
public class XMLToObject {
public static void main(String[] args) throws Exception
{
File file = new File("employee.xml");
JAXBContext obj = JAXBContext.newInstance(Employee.class);
Unmarshaller objUn = obj.createUnmarshaller();
Employee e=(Employee) objUn.unmarshal(file);
System.out.println(e.getId()+" "+e.getName()+"
"+e.getSalary());
}//End of main method
}//End of class
Output
1 Abc 50000.0

You might also like