Program to Marshall and Unmarshall list of object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package jaxb; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessType; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "subscriber") public class Subscriber { public Subscriber() {} private String firstName; private String lastName; private String type; //Prepaid or postpaid customer public Subscriber(String firstName, String lastName, String type) { super(); this.firstName = firstName; this.lastName = lastName; this.type = type; } 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 String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Subscriber [firstName=" + firstName + ", lastName=" + lastName + ", type=" + type + "]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package jaxb; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessType; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "subscribers") public class Subscribers { @XmlElement(name = "subscriber") private List<Subscriber> subscribers = new ArrayList<Subscriber>(); public Subscribers() {} public Subscribers(List<Subscriber> subscribers) { this.subscribers = subscribers; } public List<Subscriber> getSubscribers() { return subscribers; } public void setSubscribers(List<Subscriber> subscribers) { this.subscribers = subscribers; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package jaxb; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JAXBMarsheller { public static void main(String[] args) throws JAXBException { Subscriber sub1 = new Subscriber("sachin","tendulkar", "postpaid"); Subscriber sub2 = new Subscriber("pawan","kalyan", "postpaid"); Subscriber sub3 = new Subscriber("sree","nath", "prepaid"); List<Subscriber> subscribers = new ArrayList<Subscriber>(); subscribers.add(sub1); subscribers.add(sub2); subscribers.add(sub3); //Call marshall method to convert java objects to XML. System.out.println("Marshalling : Converting java object's into XML ........ "); String xml= marshall(subscribers, Subscribers.class); System.out.println(xml); //Call Unmarshal method to convert XML to java object's. System.out.println("UnMarshalling : Converting XML to java objects. ........ "); Object obj= unmarshall(Subscribers.class,xml); System.out.println(obj); } public static String marshall(List<Subscriber> subscribers, Class objectClass) throws JAXBException{ // create JAXB context and initializing Marshaller JAXBContext jaxbCtxt = JAXBContext.newInstance(objectClass); Marshaller jaxbMarshaller = jaxbCtxt.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter stringWriterObj = new StringWriter(); // Converting java object to xml using JAXB marshal API jaxbMarshaller.marshal(new Subscribers(subscribers), stringWriterObj); String xmlString = stringWriterObj.toString(); return xmlString; } public static Object unmarshall(Class objectClass, String xml) throws JAXBException{ Subscribers subscribers = new Subscribers(); // Creating an Unmarshaller JAXBContext jaxbCtxt = JAXBContext.newInstance(objectClass); Unmarshaller jaxbUnmarshaller = jaxbCtxt.createUnmarshaller(); StringReader sr = new StringReader(xml); //Converting XML to java object using JAXB unmarshal API. subscribers= (Subscribers)jaxbUnmarshaller.unmarshal(sr); return subscribers.getSubscribers(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Marshalling : Converting java object's into XML ........ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <subscribers> <subscriber> <firstName>sachin</firstName> <lastName>tendulkar</lastName> <type>postpaid</type> </subscriber> <subscriber> <firstName>pawan</firstName> <lastName>kalyan</lastName> <type>postpaid</type> </subscriber> <subscriber> <firstName>sree</firstName> <lastName>nath</lastName> <type>prepaid</type> </subscriber> </subscribers> UnMarshalling : Converting XML to java objects. ........ [Subscriber [firstName=sachin, lastName=tendulkar, type=postpaid], Subscriber [firstName=pawan, lastName=kalyan, type=postpaid], Subscriber [firstName=sree, lastName=nath, type=prepaid]] |