Hello everyone,
Today, I would like to show you how to read and XML using JAXB library.
0-Make sure your project has the library
1- We need to create the object for JAXBContext.
2- We need also to create Unmarshaller objects that will be used to
unmarshal method loading the content of the file.
package thiagolguimaraes;
/**
* @author Thiago leoncio
*
*This is JAXB Unmarshalling Example: Converting XML into Object
*
*The steps to convert XML document in the java object are:
*
*Create POJO or bind the schema and generate the classes
*Create the JAXBContext object
*Create the Unmarshaller objects
*Call the unmarshal method
*Use getter methods of POJO to access the data
*
*/
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class
XmlToObject {
public static void main(String[] args) {
try {
File file = new File("src\\thiagolguimaraes\\question.xml");
//Create the JAXBContext object
JAXBContext jaxbContext = JAXBContext.newInstance(Retrieve.class);
//Create the Unmarshaller objects
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
//Use getter methods of POJO to access the data
Retrieve que= (Retrieve) jaxbUnmarshaller.unmarshal(file);
System.out.println(que.getId()+" "+que.getRetrievename());
System.out.println("Responses:");
List<Response> list=que.getResponses();
for(Response ans:list)
System.out.println(ans.getId()+" "+ans.getResponsename()+" "+ans.getPostedby());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
---------------------------------------------------------------------------------------------------
/**
*
*/
package thiagolguimaraes;
/**
* @author Thiago Leoncio
*
*/
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
Retrieve {
private int id;
private String questionname;
private List<Response> answers;
public Retrieve() {}
public Retrieve(int id, String questionname, List<Response> 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 getRetrievename() {
return questionname;
}
public void setRetrievename(String questionname) {
this.questionname = questionname;
}
@XmlElement
public List<Response> getResponses() {
return answers;
}
public void setResponses(List<Response> answers) {
this.answers = answers;
}
}
---------------------------------------------------------------------------------------------------
/**
*
*/
package thiagolguimaraes;
/**
* @author Thiago Leoncio
*
*/
public class
Response {
private int id;
private String answername;
private String postedby;
public Response() {}
public Response(int id, String answername, String postedby) {
super();
this.id = id;
this.answername = answername;
this.postedby = postedby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getResponsename() {
return answername;
}
public void setResponsename(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}
}
Happy coding,
Thiago Leoncio.