Saturday, April 12, 2014

JAXB -- ITS VERY SIMPLE STEP BY STEP

JAXB - Converting XML to java object and vice versa.

Marshaling - java Object to XML
UnMarshalling - XML to Java Object

Steps :

1 ) 

package com.model;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Students123")
public class Student {
@XmlElement(name = "age" ,required=true)
int age;
@XmlElement(name = "name" )
String name;

ArrayList<String> skill;

ArrayList<Skill> arrayListSk;
public ArrayList<Skill> getArrayListSk() {
return arrayListSk;
}

public void setArrayListSk(ArrayList<Skill> arrayListSk) {
this.arrayListSk = arrayListSk;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public ArrayList<String> getSkill() {
return skill;
}

public void setSkill(ArrayList<String> skill) {
this.skill = skill;
}


}

2) Skill.java
package com.model;

public class Skill {

String skillName;
int skillId;
public int getSkillId() {
return skillId;
}
public void setSkillId(int skillId) {
this.skillId = skillId;
}
public String getSkillName() {
return skillName;
}
public void setSkillName(String skillName) {
this.skillName = skillName;
}
}

3)
JAXB Marshaling


package com;

import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.model.Skill;
import com.model.Student;

public class JAVA2XML {
public static void main(String[] args) throws JAXBException {
Student student = new Student();
student.setAge(77);
student.setName("kiran");
ArrayList<String > alPhone=new ArrayList<String>();
alPhone.add("111");
alPhone.add("222");
student.setSkill(alPhone);
Skill skill=new Skill();
skill.setSkillId(11);
skill.setSkillName("java");
Skill skill1=new Skill();
skill1.setSkillId(11111);
skill1.setSkillName("java11");
ArrayList<Skill> arrayListss=new ArrayList<Skill>();
arrayListss.add(skill);
arrayListss.add(skill1);
student.setArrayListSk(arrayListss);
// converting java object into XML
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();

jaxbMarshaller.marshal(student, writer);
System.out.println(writer.toString());
}
}

4) Try below code for unmarshaling by your own

               JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
       Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
       User user = (User) jaxbUnmarshaller.unmarshal(new StringReader(apiOutput));
        
//Verify the populated object
       System.out.println(user.getId());
       System.out.println(user.getFirstName());
       System.out.println(user.getLastName());



ANOTHER EXAMPLE


Consider the following JAXB class:
import javax.xml.bind.annotation.*;
 
@XmlRootElement
public class Book {
 
  @XmlElement
  private String author;
 
  @XmlElement
  private String title;
}
Unmarshalling:
To convert an XML string into an object of class Book:
public static Book unmarshal(final String xml) throws JAXBException {
  return (Book) JAXBContext.newInstance(Book.class)
                           .createUnmarshaller()
                           .unmarshal(new StringReader(xml));
}
Marshalling:
To convert a Book object into an XML string:
public static String marshal(Book book) throws JAXBException {
  final Marshaller m = JAXBContext.newInstance(Book.class)
                                  .createMarshaller();
  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  final StringWriter w = new StringWriter();
  m.marshal(book, w);
  return w.toString();
}


No comments: