0 Replies Latest reply: Oct 4, 2012 4:06 AM by denixxbaykin RSS

    JAXB, @XmlType(propOrder = { ... }) and @XmlAttribute

    denixxbaykin
      Hi.
      Preamble: we are working with governmental authority, and we are using XML-files for information transfer.
      They (authority) support only strict elements and attributes order in files (not only for elements but also for attributes!). :(
      Now we are using manual assembly of XML-file.

      I found this JAXB example: http://www.vogella.com/articles/JAXB/article.html
      Here I found "@XmlType(propOrder = { "field2", "field1",.. })".
      I tried this and get first results - elements were in order that I specify.
      When I annotated 4 of 8 fields with @XmlAttribute, it also works fine, but I should inverse the order of attributes in propOrder - it's ok.
      It looks like I can use JAXB for my task.
      I use Java SE 1.6.0_35.

      Question is:
      Will this behaviour be as is in further releases of Java SE? In 1.7, 1.8 and so on.

      Test code:
      -----

      package com.mycompany.test8_jaxb_with_ordering;

      import javax.xml.bind.annotation.XmlAttribute;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlType;

      @XmlRootElement(name = "TestClass")
      @XmlType(propOrder = { "test2", "test3", "test4", "test1", "test5", "test7", "test6", "test8" })
      public class TestClass {
      private String test1 = "test1&_text";
      private String test3 = "test3\"_text";
      private String test2 = "test2_text";
      private String test4 = "test4_text";
      private String test5 = "test5&_text";
      private String test7 = "test7\"_text";
      private String test6 = "test6_text";
      private String test8 = "test8_text";

      @XmlAttribute
      public String getTest1() {
      return test1;
      }

      @XmlAttribute
      public String getTest2() {
      return test2;
      }

      @XmlAttribute
      public String getTest3() {
      return test3;
      }

      @XmlAttribute
      public String getTest4() {
      return test4;
      }

      public String getTest5() {
      return test5;
      }

      public String getTest6() {
      return test6;
      }

      public String getTest7() {
      return test7;
      }

      public String getTest8() {
      return test8;
      }

      public void setTest1(String test1) {
      this.test1 = test1;
      }

      public void setTest2(String test2) {
      this.test2 = test2;
      }

      public void setTest3(String test3) {
      this.test3 = test3;
      }

      public void setTest4(String test4) {
      this.test4 = test4;
      }

      public void setTest5(String test5) {
      this.test5 = test5;
      }

      public void setTest6(String test6) {
      this.test6 = test6;
      }

      public void setTest7(String test7) {
      this.test7 = test7;
      }

      public void setTest8(String test8) {
      this.test8 = test8;
      }
      }
      -----

      package com.mycompany.test8_jaxb_with_ordering;

      import java.io.FileReader;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.io.Writer;
      import javax.xml.bind.*;

      public class App {

      private static final String TEST_XML = "./test-jaxb.xml";

      public static void main(String[] args) throws PropertyException, JAXBException, IOException {
      TestClass tc = new TestClass();

      JAXBContext context = JAXBContext.newInstance(TestClass.class);
      Marshaller m = context.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      m.marshal(tc, System.out);

      Writer w = null;
      try {
      w = new FileWriter(TEST_XML);
      m.marshal(tc, w);
      } finally {
      try {
      w.close();
      } catch (Exception e) {
      }
      }

      // get variables from our xml file, created before
      System.out.println();
      System.out.println("Output from our XML File: ");
      Unmarshaller um = context.createUnmarshaller();
      TestClass tc2 = (TestClass) um.unmarshal(new FileReader(TEST_XML));

      System.out.println("tc2.test1 = "+tc2.getTest1());
      System.out.println("tc2.test2 = "+tc2.getTest2());
      System.out.println("tc2.test3 = "+tc2.getTest3());
      System.out.println("tc2.test4 = "+tc2.getTest4());
      System.out.println("tc2.test5 = "+tc2.getTest5());
      System.out.println("tc2.test6 = "+tc2.getTest6());
      System.out.println("tc2.test7 = "+tc2.getTest7());
      System.out.println("tc2.test8 = "+tc2.getTest8());

      }
      }
      -----