Friday, December 6, 2019

XML to java Object converter usinn Marshall class and Vise Versa

Converting the XML to Java object instead reading the xml file every time. It will be good approached in terms of performance.




Lets say below is the xml to convert into Object 

    
 







Pre-requests : Includes below dependencies in POM.xml file 









2nd Steps : to  create the  POJO Class  having JAXB Annotation . 
For object that need to convert to / from XML file, it have to annotate with JAXB annotation.

Below is the class format for POJO Class:

import java.util.List;  
  
import javax.xml.bind.annotation.XmlAttribute;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  
  
@XmlRootElement  
public class Question {  
private int id;  
private String questionname;  
private List answers;  
public Question() {}  
public Question(int id, String questionname, List answers) {  
    super();  
    this.id = id;  
    this.questionname = questionname;  
    this.answers = answers;  
}  
@XmlAttribute  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
@XmlElement  
public String getQuestionname() {  
    return questionname;  
}  
public void setQuestionname(String questionname) {  
    this.questionname = questionname;  
}  
@XmlElement  
public List getAnswers() {  
    return answers;  
}  
public void setAnswers(List answers) {  
    this.answers = answers;  
}  
}  


NOTE: 
The Best option to create a such class is "Online tools which generate code , Just search in google with 'XML to JAVA Converter'  " . refer https://codebeautify.org/xml-to-java-converter





3rd Steps. Convert Object to XML: 
JAXB marshalling example, convert customer object into a XML file. The jaxbMarshaller.marshal() contains a lot of overloaded methods, find one that suit your output.



public static void main(String[] args)
   throws JAXBException, SAXException, IOException, ParserConfigurationException {

  JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  // file readers
  InputStream inputStream = new FileInputStream(
    "xmltoobject\\NewFile.xml");
  Reader reader = new InputStreamReader(inputStream, "UTF-8");
  Question customer = null;
  try {
   customer = (Question) jaxbUnmarshaller.unmarshal(reader);

  } catch (Exception e) {
   e.printStackTrace();
  }
  System.out.println(customer.environment); }}
  to get the Below out put :System.out.println(que.getId()+" "+que.getQuestionname());  
        System.out.println("Answers:");  
        List list=que.getAnswers();  
        for(Answer ans:list)  
          System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  



Out Put : 1 What is java?
          Answers:
         101 java is a programming language ravi
         102 java is a platform john









3th Steps . Convert Object to XML  (ViseVersa):


JAXB marshalling example, convert customer object into a XML file. The jaxbMarshaller.marshal() contains a lot of overloaded methods, find one that suit your output.



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("mkyong");
   customer.setAge(29);

   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();
       }

 }
}


Out put is XML file 

Reference article : https://www.mkyong.com/java/jaxb-hello-world-example/
https://www.javatpoint.com/jaxb-unmarshalling-example




No comments:

Post a Comment