title: JAXB XML output format questions
body: <p>I have Java classes with the following structure (the class names do not imply anything, I was just making them up).</p>
  
  <pre><code>package test;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  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 javax.xml.bind.annotation.XmlValue;
  
  @XmlRootElement
  public class Test
  {
      @XmlAccessorType(XmlAccessType.FIELD)
      static class Machine
      {
          @XmlElementWrapper(name="servers")
          @XmlElement(name="server")
          List&lt;Server&gt; servers = new ArrayList&lt;Server&gt;();
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      static class Server
      {
          Threshold t = new Threshold();
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      static class Threshold
      {
          RateThreshold load = new RateThreshold();
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      static class RateThreshold
      {
          @XmlAccessorType(XmlAccessType.FIELD)
          static class Rate
          {
              int count;
              Period period = new Period();
          }
  
          @XmlAccessorType(XmlAccessType.FIELD)
          private static class Period
          {
              @XmlAttribute
              private String type = "second";
  
              @XmlValue
              private float period;
          }
  
          Rate min = new Rate();
          Rate max = new Rate();
      }
  
      @XmlElementWrapper(name="machines")
      @XmlElement(name="machine")
      List&lt;Machine&gt; machines = new ArrayList&lt;Machine&gt;();
  
      public static void main(String[] args)
      {
          Machine m = new Machine();
          Server s = new Server();
          s.t.load.max.count = 10;
          s.t.load.min.count = 1;
          m.servers.add(s);
  
          Test t = new Test();
          t.machines.add(m);
  
          JAXBContext jaxbContext;
          Marshaller marshaller;
          try
          {
              jaxbContext = JAXBContext.newInstance(Test.class);
              marshaller = jaxbContext.createMarshaller();
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              marshaller.marshal(t, System.out);
          }
          catch (JAXBException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The problem I am having is with the XML output generated by JAXB when marshalling a Test instance.  The XML output would always look like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;test&gt;
      &lt;machines&gt;
          &lt;machine&gt;
              &lt;servers&gt;
                  &lt;server&gt;
                      &lt;t&gt;
                          &lt;load&gt;
                              &lt;min&gt;
  &lt;count&gt;1&lt;/count&gt;
  &lt;period type="second"&gt;0.0&lt;/period&gt;
                              &lt;/min&gt;
                              &lt;max&gt;
  &lt;count&gt;10&lt;/count&gt;
  &lt;period type="second"&gt;0.0&lt;/period&gt;
                              &lt;/max&gt;
                          &lt;/load&gt;
                      &lt;/t&gt;
                  &lt;/server&gt;
              &lt;/servers&gt;
          &lt;/machine&gt;
      &lt;/machines&gt;
  &lt;/test&gt;
  </code></pre>
  
  <p>As you can see, some elements are not being indented properly (that is, the deepest elements, count and period).  Why is that?  Is there something wrong with the way I created the JAXB context?  Or is there a maximum limit to how many elements that can be indented recursively by JAXB?  How could I fix this?  Note that I have also set JAXB_FORMATTED_OUTPUT to true, but still get the improper indentation.</p>
  
  <p>Thanks.</p>