Here I am providing code with JAXB for converting java object to xml and xml to java objects.
Java Class to convert to xml
package com.bj.file.test.jaxb;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String firstName;
private String lastName;
private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
Java Class to convert to xml
package com.bj.file.test.jaxb;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String firstName;
private String lastName;
private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
package com.bj.file.test.jaxb;
public class PhoneNumber {
private String type;
private String number;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
Now a demo class which shows how can you convert xml to Java object and Java object to XML.
Here I have provided a method genericReadingInput() which convert all the objects to corrosponding XML without knowing the objects name. It will take the root tag as the object class name and other parameters as element of xml.
package com.bj.file.test.jaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
public class Demo {
// This will allow only specific objects.
private void readingInput() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
StreamSource xml = new StreamSource("src/com/bj/file/test/jaxb/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Customer> je1 = unmarshaller.unmarshal(xml, Customer.class);
Customer customer = je1.getValue();
JAXBElement<Customer> je2 = new JAXBElement<Customer>(new QName("Customer"), Customer.class, customer);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(je2, System.out);
}
// This will allow generic objects in place of specific objects.
private void genericReadingInput() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
StreamSource xml = new StreamSource("src/com/bj/file/test/jaxb/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Customer> je1 = unmarshaller.unmarshal(xml, Customer.class);
// Now convert this to a plain java object
Object obj = je1.getValue();
// Here you do not need to know the name of java objects class name So you can use this code for any object.
JAXBElement<? extends Object> je2 = new JAXBElement<Object>(new QName(getClassName(obj)), Object.class, obj);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(je2, System.out);
}
//This will allow to write xml from java object without annotation.
private void convertJavaObj2Xml() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Customer customer = new Customer();
customer.setFirstName("Lyoubomir");
customer.setLastName("Veltchev");
PhoneNumber workPhone = new PhoneNumber();
workPhone.setType("work");
workPhone.setNumber("555-1111");
customer.getPhoneNumbers().add(workPhone);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"), Customer.class, customer);
marshaller.marshal(rootElement, System.out);
}
private String getClassName(Object obj) {
return obj.getClass().getSimpleName();
}
public static void main(String[] args) throws Exception {
Demo demo = new Demo();
demo.convertJavaObj2Xml();
System.out.println("\n\n\n\n");
demo.readingInput();
demo.genericReadingInput();
}
}