title: How to create an element inside another in java using JAXB?
body: <p>I have a java code i'm using to create XML file using JAXB, i know how to create root elements and elements but i want to create a child elements from an element like that:</p>
  
  <pre><code>    &lt;root element&gt;
          &lt;element&gt;
                 &lt;child element&gt;
                 &lt;group of elements and attributes&gt;
                 &lt;/child element&gt;
          &lt;/element&gt;
      &lt;/root element&gt;
  </code></pre>
  
  <p>I'm now only know how to create like this:</p>
  
  <pre><code>    &lt;XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA"&gt;
          &lt;hostName&gt;local.yahoo.com&lt;/hostName&gt;
          &lt;parameters&gt;
              &lt;entry&gt;
                  &lt;key&gt;csz&lt;/key&gt;
                  &lt;value&gt;Cairo,+CA&lt;/value&gt;
              &lt;/entry&gt;
          &lt;/parameters&gt;
          &lt;urlPath&gt;/rss/restaurants&lt;/urlPath&gt;
      &lt;/XmlSource&gt; 
  </code></pre>
  
  <p>So, what will i do if i want to put the parameters inside the hostName to be like that :</p>
  
  <pre><code>&lt;XmlSource URL="http://local.yahoo.com/rss/restaurants?csz=Cairo,+CA"&gt;
      &lt;hostName name="local.yahoo.com"&gt;
               &lt;parameters&gt;
                  &lt;entry&gt;
                      &lt;key&gt;csz&lt;/key&gt;
                      &lt;value&gt;LosAngelos,+CA&lt;/value&gt;
                  &lt;/entry&gt;
              &lt;/parameters&gt;
     &lt;/hostName&gt;
      &lt;urlPath&gt;/rss/restaurants&lt;/urlPath&gt;
  &lt;/XmlSource&gt;
  </code></pre>
  
  <p>And finally that's the java code i use :</p>
  
  <p>The class i put annotations at :</p>
  
  <pre><code>       @XmlRootElement(name= "Source")
         public class XmlConf {
  
      @XmlElement(name= "Source")
  
      private URL url;
      private List&lt;String&gt; path = new ArrayList&lt;String&gt;();
      private String urlp;
      private Map&lt;String, String&gt; parameters;
      private String host;
  
  
      public URL getUrl() {
          return url;
      }
      @XmlAttribute(name = "URL")
      public void setUrl(URL url) {
          this.url = url;
      }
      @XmlElement
      public List&lt;String&gt; getPath() {
          return path;
      }
      public void setPath(String path) {
          this.path.add(path);
      }
      @XmlElement
      public void setUrlPath(String urlp){
          this.urlp = urlp;
      }
      public String getUrlPath(){
          return urlp;
      }
      @XmlElement(name = "param")
      public void setParameters(Map&lt;String, String&gt; parameters){
          this.parameters = parameters;
      }
      public Map&lt;String, String&gt; getParameters(){
          return parameters;
      }
      public void setHostName(String host){
          this.host = host;
      }
      public String getHostName(){
          return host;
      }
        }
  </code></pre>
  
  <p>the class i use JAXB in :</p>
  
  <pre><code>    public class ConfList {
  
      private static final String fileName = "Source.xml";
      List&lt;String&gt; xmlConfList;
      private Object object;
  
      public ConfList(Object object){
          this.object = object;
      }
  
      public void addToList() throws IOException, JAXBException {
  
          File file = new File(object+fileName);
          JAXBContext jaxbContext = JAXBContext.newInstance(XmlConf.class);
          Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
          jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
          jaxbMarshaller.marshal(object, file);
          jaxbMarshaller.marshal(object, System.out);
  
      }
        }
  </code></pre>