Sometimes we need to convert a Java object into an XML file. This can be achieved using JAXB. (JDK1.6 and later versions do not need to import dependency packages because they are already included in the JDK)
If a company has many departments, and there are many employees in each department, we can design simple bean objects like this.
[Java]View PlainCopy
- @XmlRootElement (name="department")
- Public class Department {
- private String name; //Department name
- Private list<staff> staffs; //Actually staff is comprehended, here is plus ' s ' is to distinguish staff
- Public String GetName () {
- return name;
- }
- @XmlAttribute
- public void SetName (String name) {
- this.name = name;
- }
- Public list<staff> getstaffs () {
- return staffs;
- }
- @XmlElement (name="Staff")
- public void Setstaffs (list<staff> staffs) {
- this.staffs = staffs;
- }
- }
[Java]View PlainCopy
- @XmlRootElement (name="Staff")
- Public class Staff {
- private String name; //Staff name
- private int age; //Staff age
- private Boolean smoker; /Is it a smoker ?
- Public String GetName () {
- return name;
- }
- @XmlElement
- public void SetName (String name) {
- this.name = name;
- }
- public int getage () {
- return age;
- }
- @XmlElement
- public void Setage (int.) {
- this.age = age;
- }
- Public Boolean issmoker () {
- return smoker;
- }
- @XmlAttribute
- public void Setsmoker (boolean smoker) {
- this.smoker = smoker;
- }
- }
The following will generate a simple object and convert it into an XML string.
[Java]View PlainCopy
- Public class Main {
- public static void Main (string[] args) throws Exception {
- Jaxbcontext context = Jaxbcontext.newinstance (Department. Class,staff. class); //Get Context object
- Marshaller Marshaller = Context.createmarshaller (); //Get Marshaller object based on context
- Marshaller.setproperty (marshaller.jaxb_encoding, "UTF-8"); //Set coded character sets
- Marshaller.setproperty (Marshaller.jaxb_formatted_output, true); //formatted XML output, with branches and indents
- Marshaller.marshal (Getsimpledepartment (), System.out); //Print to console
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- Marshaller.marshal (Getsimpledepartment (), BAOs);
- String xmlobj = new String (Baos.tobytearray ()); //Generate XML string
- System.out.println (Xmlobj);
- }
- /**
- * Generate a simple Department object
- * @return
- */
- private static Department getsimpledepartment () {
- list<staff> staffs = new arraylist<staff> ();
- Staff STF = new staff ();
- Stf.setname ("Jay Chou");
- Stf.setage (30);
- Stf.setsmoker (false);
- Staffs.add (STF);
- Stf.setname ("weekly Pen Chang");
- Stf.setage (28);
- Stf.setsmoker (false);
- Staffs.add (STF);
- Stf.setname ("Stephen Chow Chi");
- Stf.setage (40);
- Stf.setsmoker (true);
- Staffs.add (STF);
- Department Dept = New Department ();
- Dept.setname ("Entertainment Circle");
- Dept.setstaffs (staffs);
- return dept;
- }
- }
Console printing information:
[Plain]View PlainCopy
- <?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
- <department name= "Entertainment Circle" >
- <staff smoker= "true" >
- <age>40</age>
- <name> Stephen Chow </name>
- </staff>
- <staff smoker= "true" >
- <age>40</age>
- <name> Stephen Chow </name>
- </staff>
- <staff smoker= "true" >
- <age>40</age>
- <name> Stephen Chow </name>
- </staff>
- </department>
Notice that we can convert an object to an XML file using the Marshaller.marshal method, or we can convert the XML to an object using Unmarshaller.unmarshal.
Java Objects and XML transformations