title: How to create nested Root Element in JAXB when creating xml from Java object using JAXB
body: <pre><code>**XML to be generated from java object**  
   &lt;automobiles&gt;
      &lt;cars&gt;
          &lt;type&gt;&lt;/type&gt;
          &lt;car&gt;
              &lt;model&gt;&lt;/model&gt;
              &lt;maxspeed&gt;&lt;/maxspeed&gt;
          &lt;/car&gt;
          &lt;car&gt;
              &lt;model&gt;&lt;/model&gt;
              &lt;maxspeed&gt;&lt;/maxspeed&gt;
          &lt;/car&gt;
      &lt;/cars&gt;
      &lt;bikes&gt;
          &lt;type&gt;&lt;/type&gt;
          &lt;bike&gt;
              &lt;model&gt;&lt;/model&gt;
              &lt;maxspeed&gt;&lt;/maxspeed&gt;
          &lt;/bike&gt;
          &lt;bike&gt;
              &lt;model&gt;&lt;/model&gt;
              &lt;maxspeed&gt;&lt;/maxspeed&gt;
          &lt;/bike&gt;
      &lt;/bikes&gt;
  &lt;/automobiles&gt;
  </code></pre>
  
  <p>// the object class to hold values for the xml is</p>
  
  <pre><code>@XmlRootElement(name = "automobiles")
  class Automobiles {
      private List&lt;Cars&gt; cars = null;
      private List&lt;Bikes&gt; bikes = null;
  
      @XmlElement
      public List&lt;Bikes&gt; getBikes() {
          return bikes;
      }
  
      public void setBikes(List&lt;Bikes&gt; bikes) {
          this.bikes = bikes;
      }
  
      @XmlElement
      public List&lt;Cars&gt; getCars() {
          return cars;
      }
  
      public void setCars(List&lt;Cars&gt; cars) {
          this.cars = cars;
      }
  }
  
  @XmlRootElement(name = "cars")
  class Cars {
      private List&lt;Car&gt; car = null;
  
      @XmlElement
      public List&lt;Car&gt; getCar() {
          return car;
      }
  
      public void setCar(List&lt;Car&gt; car) {
          this.car = car;
      }
  }
  
  @XmlRootElement(name = "bikes")
  class Bikes {
      private List&lt;Bike&gt; bike = null;
  
      @XmlElement
      public List&lt;Bike&gt; getBikes() {
          return bike;
      }
  
      public void setBikes(List&lt;Bike&gt; bike) {
          this.bike = bike;
      }
  }
  
  @XmlRootElement(name = "Car")
  class Car {
      private String model = null;
      private String maxspeed = null;
  
      @XmlElement
      public String getModel() {
          return model;
      }
  
      public void setModel(String model) {
          this.model = model;
      }
  
      @XmlElement
      public String getMaxspeed() {
          return maxspeed;
      }
  
      public void setMaxspeed(String maxspeed) {
          this.maxspeed = maxspeed;
      }
  }
  
  @XmlRootElement(name = "Bike")
  class Bike {
      private String model = null;
      private String maxspeed = null;
  
      @XmlElement
      public String getModel() {
          return model;
      }
  
      public void setModel(String model) {
          this.model = model;
      }
  
      @XmlElement
      public String getMaxspeed() {
          return maxspeed;
      }
  
      public void setMaxspeed(String maxspeed) {
          this.maxspeed = maxspeed;
      }
  }
  </code></pre>
  
  <p>Here the above xml is the format I need to generate using JAXB marshaling.
  I'm new to JAXB. Is the object class and annotation that I have used is correct? can someone please help me out on this.</p>