title: Creating XML in constructors
body: <p>so I'm working on an existing project and I'm trying to refactor it. It uses XML, and the current code looks something like this:</p>
  
  <p>There is a class that's used to produce the XML:</p>
  
  <pre><code>import java.util.List;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementWrapper;
  import javax.xml.bind.annotation.XmlRootElement;
  import hr.pbz.jhex.engine.xml.XmlMarshaller;
  
  @XmlRootElement(name = "root")
  public class MyXmlClass extends XmlMarshaller{
  
      private String string1;
      private String string2;
  
      public String getString1() {
          return string1;
      }
  
      @XmlAttribute
      public void setString1(String string1) {
          this.string1 = string1;
      }
  
      public String getString2() {
          return string2;
      }
  
      @XmlAttribute
      public void setString2(String string2) {
          this.string2 = string2;
      }
  
  }
  </code></pre>
  
  <p>and it's used in the Main.java </p>
  
  <pre><code>MyXmlClass myXml = new MyXmlClass()
  myXml.setString1("foo");
  myXml.SetString2("bar");
  </code></pre>
  
  <p>This works like it's supposed to, and produces good results.</p>
  
  <p>The problem is that in my real codes, there are much more field than just string1, string2, and that just setting the XML uses a lot of lines of code.</p>
  
  <p>My question is, can I use constructor in the MyXmlClass? Something like:</p>
  
  <pre><code>import java.util.List;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementWrapper;
  import javax.xml.bind.annotation.XmlRootElement;
  import hr.pbz.jhex.engine.xml.XmlMarshaller;
  
  @XmlRootElement(name = "root")
  public class MyXmlClass extends XmlMarshaller{
  
      private String string1;
      private String string2;
  
      public MyXmlClass() { //doesn't work without the default noargs constructor
      }
  
      public MyXmlClass(String string1, String string2) {
             this.string1 = string1;
             this.string2 = string2;
      }
  
      public String getString1() {
          return string1;
      }
  
      @XmlAttribute
      public void setString1(String string1) {
          this.string1 = string1;
      }
  
      public String getString2() {
          return string2;
      }
  
      @XmlAttribute
      public void setString2(String string2) {
          this.string2 = string2;
      }
  
  }
  </code></pre>
  
  <p>and then use it in Main.java like:</p>
  
  <pre><code>MyXmlClass myXml = new MyXmlClass("foo", "bar");
  </code></pre>
  
  <p>I have tried it, and it produces mixed results. Sometimes it works like it should, and in other places not so much.</p>
  
  <p>===EDIT===</p>
  
  <p>Should've been clearer about 'not so much' part, here's an example.</p>
  
  <p>Let's say that now I'm working with an XmlClass with more than just 2 fields, let say 4.</p>
  
  <p>I first instantiate it providing 2 parameters like this:</p>
  
  <pre><code>MyXmlClass myXml = new MyXmlClass("foo", "bar");
  </code></pre>
  
  <p>then I decide to set the third and fourth fields using setters.</p>
  
  <pre><code>myXml.setElement3("set after instantiating");
  myXml.setElement4("also after instantiating");
  </code></pre>
  
  <p>If I do it in this order, the xml does not contain the element I set calling setElement4. If I swap their places, like this:</p>
  
  <pre><code>myXml.setElement4("also after instantiating");
  myXml.setElement3("set after instantiating");
  </code></pre>
  
  <p>it works correctly, and all the elements show up. The classes I'm working with are pretty big so it would take some time setting up an MCVE for this thread, but I will try it. Posting this just to see if this is even valid, or I'm doing some hacky things that really should not be done, and that's why the program is acting weird in some cases.</p>
  
  <p>===EDIT===</p>
  
  <p>Is it even safe to do it this way, using constructors? Thanks.</p>