Java Objects and XML transformations

Source: Internet
Author: User

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
  1. @XmlRootElement (name="department")
  2. Public class Department {
  3. private String name; //Department name
  4. Private list<staff> staffs; //Actually staff is comprehended, here is plus ' s ' is to distinguish staff
  5. Public String GetName () {
  6. return name;
  7. }
  8. @XmlAttribute
  9. public void SetName (String name) {
  10. this.name = name;
  11. }
  12. Public list<staff> getstaffs () {
  13. return staffs;
  14. }
  15. @XmlElement (name="Staff")
  16. public void Setstaffs (list<staff> staffs) {
  17. this.staffs = staffs;
  18. }
  19. }

[Java]View PlainCopy
  1. @XmlRootElement (name="Staff")
  2. Public class Staff {
  3. private String name; //Staff name
  4. private int age; //Staff age
  5. private Boolean smoker; /Is it a smoker ?
  6. Public String GetName () {
  7. return name;
  8. }
  9. @XmlElement
  10. public void SetName (String name) {
  11. this.name = name;
  12. }
  13. public int getage () {
  14. return age;
  15. }
  16. @XmlElement
  17. public void Setage (int.) {
  18. this.age = age;
  19. }
  20. Public Boolean issmoker () {
  21. return smoker;
  22. }
  23. @XmlAttribute
  24. public void Setsmoker (boolean smoker) {
  25. this.smoker = smoker;
  26. }
  27. }


The following will generate a simple object and convert it into an XML string.

[Java]View PlainCopy
  1. Public class Main {
  2. public static void Main (string[] args) throws Exception {
  3. Jaxbcontext context = Jaxbcontext.newinstance (Department. Class,staff.    class); //Get Context object
  4. Marshaller Marshaller = Context.createmarshaller (); //Get Marshaller object based on context
  5. Marshaller.setproperty (marshaller.jaxb_encoding, "UTF-8"); //Set coded character sets
  6. Marshaller.setproperty (Marshaller.jaxb_formatted_output, true); //formatted XML output, with branches and indents
  7. Marshaller.marshal (Getsimpledepartment (), System.out); //Print to console
  8. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  9. Marshaller.marshal (Getsimpledepartment (), BAOs);
  10. String xmlobj = new String (Baos.tobytearray ()); //Generate XML string
  11. System.out.println (Xmlobj);
  12. }
  13. /** 
  14. * Generate a simple Department object
  15. * @return
  16. */
  17. private static Department getsimpledepartment () {
  18. list<staff> staffs = new arraylist<staff> ();
  19. Staff STF = new staff ();
  20. Stf.setname ("Jay Chou");
  21. Stf.setage (30);
  22. Stf.setsmoker (false);
  23. Staffs.add (STF);
  24. Stf.setname ("weekly Pen Chang");
  25. Stf.setage (28);
  26. Stf.setsmoker (false);
  27. Staffs.add (STF);
  28. Stf.setname ("Stephen Chow Chi");
  29. Stf.setage (40);
  30. Stf.setsmoker (true);
  31. Staffs.add (STF);
  32. Department Dept = New Department ();
  33. Dept.setname ("Entertainment Circle");
  34. Dept.setstaffs (staffs);
  35. return dept;
  36. }
  37. }


Console printing information:

[Plain]View PlainCopy
  1. <?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
  2. <department name= "Entertainment Circle" >
  3. <staff smoker= "true" >
  4. <age>40</age>
  5. <name> Stephen Chow </name>
  6. </staff>
  7. <staff smoker= "true" >
  8. <age>40</age>
  9. <name> Stephen Chow </name>
  10. </staff>
  11. <staff smoker= "true" >
  12. <age>40</age>
  13. <name> Stephen Chow </name>
  14. </staff>
  15. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.