title: How to Use Jaxb for nested POJO class to generate XML model in Java
body: <p>I'm using JAXB to create an xml data model and it seems to be working really well for non-complex XML model but as soon as I get into a slightly complex XML, JAXB doesn't seem to work as well.</p>
  
  <p>This is the XML-generation I would like to acheive:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;SampleRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="Sample.xsd"&gt;
  
      &lt;Name&gt;Sample Name&lt;/Name&gt;
      &lt;Description&gt;This is a description&lt;/Description&gt;
      &lt;Graph&gt;Graph test&lt;/Graph&gt;
  
      &lt;VerifyAttr&gt;
          &lt;Data&gt;id&lt;/Data&gt;
          &lt;Value&gt;32&lt;/Value&gt;
      &lt;/VerifyAttr&gt;
  
      &lt;VeirfyXpath&gt;
          &lt;MemXpath&gt;/Root/Name&lt;/MemXpath&gt;
          &lt;Value&gt;Mosawi&lt;/Value&gt;
      &lt;/&lt;VeirfyXpath&gt;&gt;
  
  &lt;/SampleRoot&gt;
  </code></pre>
  
  <p>This is the Nested Pojo class I created (should I not create a nested pojo? Perhaps separate them out?):</p>
  
  <pre><code>package com.sample.model;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement(name="SampleRoot")
  public class Sample {
      private String name;
      private String description;
      private String graph;
  
      public String getName() {
          return name;
      }
      @XmlElement
      public void setName(String name) {
          this.name = name;
      }
      public String getDescription() {
          return description;
      }
      @XmlElement
      public void setDescription(String description) {
          this.description = description;
      }
      public String getGraph() {
          return graph;
      }
      @XmlElement
      public void setGraph(String graph) {
          this.graph = graph;
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      public class VerifyAttr {
          private String data;
          private String value;
  
          public String getData() {
              return data;
          }
          @XmlElement
          public void setData(String data) {
              this.data = data;
          }
          public String getValue() {
              return value;
          }
          @XmlElement
          public void setValue(String value) {
              this.value = value;
          }
  
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      public class VerifyXpath {
          private String memXpath;
          private String value;
  
          public String getMemXpath() {
              return memXpath;
          }
          @XmlElement
          public void setMemXpath(String memXpath) {
              this.memXpath = memXpath;
          }
          public String getValue() {
              return value;
          }
          @XmlElement
          public void setValue(String value) {
              this.value = value;
          }
  
      }
  
  }
  </code></pre>
  
  <p>And here is the demo to exercise the pojo model:</p>
  
  <pre><code>public static void main(String[] args) {
      Sample sample = new Sample();
  
      sample.setName("Name");
      sample.setDescription("This is a description");
      sample.setGraph("Graph Test");
  
      VerifyAttr va = sample.new VerifyAttr();
      va.setData("id");
      va.setValue("32");
  
      VerifyXpath vx = sample.new VerifyXpath();
      vx.setMemXpath("/Root/Name");
      vx.setValue("Mosawi");
  
  
      try {
          JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class);
          Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
          jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
          jaxbMarshaller.marshal(sample, new File("Sample.xml") );
          jaxbMarshaller.marshal(sample, System.out);
  
      } catch(Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The problem I have is that the full XML as defined above is not generated. This is what is generated:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;SampleRoot&gt;
      &lt;description&gt;This is a description&lt;/description&gt;
      &lt;graph&gt;Graph Test&lt;/graph&gt;
      &lt;name&gt;Name&lt;/name&gt;
  &lt;/SampleRoot&gt;
  </code></pre>
  
  <p>What am I doing wrong? Why am I not getting the model XML as I should above? The problem is with <code>demo main</code>?</p>