title: JAXB only marshals/unmarshals root node
body: <p>I am new to JAXB. All I want to do is, given an existing xml string marshal it, and return it in an ejb. The problem is the following code only seems to work for the root node, and not the children. I would like it to work for the whole thing. If it is any help, I have not altered any of the code generated by feeding in the scheme. Any pointers would be helpful, thanks.</p>
  
  <pre><code>import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.ejb.Stateless;
  import javax.jws.WebService;
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.JAXBException;
  import org.netbeans.j2ee.wsdl.test.newwsdl.NewWSDLPortType;
  import org.netbeans.xml.schema.newxmlschema.*;
  
  @WebService(serviceName = "newWSDLService", portName = "newWSDLPort", endpointInterface = "org.netbeans.j2ee.wsdl.test.newwsdl.NewWSDLPortType", targetNamespace = "http://j2ee.netbeans.org/wsdl/test/newWSDL", wsdlLocation = "META-INF/wsdl/NewWebServiceFromWSDL/newWSDLWrapper.wsdl")
  @Stateless
  public class NewWebServiceFromWSDL implements NewWSDLPortType {
  
      public Root newWSDLOperation() {
          String xml = 
          "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;"
          +"&lt;ns0:root  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
          +"   xmlns:ns0='http://xml.netbeans.org/schema/newXmlSchema'"
          +"   xsi:schemaLocation='http://xml.netbeans.org/schema/newXmlSchema newXmlSchema.xsd'&gt;"
          +"    &lt;ns0:node&gt;"
          +"        &lt;ns0:name&gt;asdf&lt;/ns0:name&gt;"
          +"        &lt;ns0:value&gt;asdf&lt;/ns0:value&gt;"
          +"        &lt;ns0:date&gt;2009-01-01&lt;/ns0:date&gt;"
          +"    &lt;/ns0:node&gt;"
          +"    &lt;ns0:node&gt;"
          +"        &lt;ns0:name&gt;asdf&lt;/ns0:name&gt;"
          +"        &lt;ns0:value&gt;asdf&lt;/ns0:value&gt;"
          +"        &lt;ns0:date&gt;2009-01-01&lt;/ns0:date&gt;"
          +"    &lt;/ns0:node&gt;"
          +"    &lt;ns0:node&gt;"
          +"        &lt;ns0:name&gt;asdf&lt;/ns0:name&gt;"
          +"        &lt;ns0:value&gt;asdf&lt;/ns0:value&gt;"
          +"        &lt;ns0:date&gt;2009-01-01&lt;/ns0:date&gt;"
          +"    &lt;/ns0:node&gt;"
          +"&lt;/ns0:root&gt;";
          Root root = null;
          try {
              root = jaxbUnmarshalFromString(xml);
          } catch (Throwable ex) {
              Logger.getLogger(NewWebServiceFromWSDL.class.getName()).log(Level.SEVERE, null, ex);
          }
          return root;
      }
  
      private Root jaxbUnmarshalFromString(String str) throws javax.xml.bind.JAXBException {
          Root ret = null;
          javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(Root.class.getPackage().getName());
          javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
          ret = (Root) ((JAXBElement)unmarshaller.unmarshal(new java.io.StringReader(str))).getValue();
  
          return ret;
      }
  
  }
  </code></pre>
  
  <p>Here is the scheme:</p>
  
  
  
  <pre><code>&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://xml.netbeans.org/schema/newXmlSchema"
      xmlns:tns="http://xml.netbeans.org/schema/newXmlSchema"
      elementFormDefault="qualified"&gt;
      &lt;xsd:complexType name="root"&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="node" type="tns:node" maxOccurs="unbounded" minOccurs="0"/&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
      &lt;xsd:complexType name="node"&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="name" type="xsd:string"/&gt;
              &lt;xsd:element name="value" type="xsd:string"/&gt;
              &lt;xsd:element name="date" type="xsd:date"/&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
      &lt;xsd:element name="root" type="tns:root"/&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>Classes Generated from scheme:</p>
  
  <pre><code>package org.netbeans.xml.schema.newxmlschema;
  
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlElementDecl;
  import javax.xml.bind.annotation.XmlRegistry;
  import javax.xml.namespace.QName;
  
  @XmlRegistry
  public class ObjectFactory {
  
      private final static QName _Root_QNAME = new QName("http://xml.netbeans.org/schema/newXmlSchema", "root");
  
      /**
       * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.netbeans.xml.schema.newxmlschema
       * 
       */
      public ObjectFactory() {
      }
  
      /**
       * Create an instance of {@link Root }
       * 
       */
      public Root createRoot() {
          return new Root();
      }
  
      /**
       * Create an instance of {@link Node }
       * 
       */
      public Node createNode() {
          return new Node();
      }
  
      /**
       * Create an instance of {@link JAXBElement }{@code &lt;}{@link Root }{@code &gt;}}
       * 
       */
      @XmlElementDecl(namespace = "http://xml.netbeans.org/schema/newXmlSchema", name = "root")
      public JAXBElement&lt;Root&gt; createRoot(Root value) {
          return new JAXBElement&lt;Root&gt;(_Root_QNAME, Root.class, null, value);
      }
  
  }
  
  
  package org.netbeans.xml.schema.newxmlschema;
  
  import java.util.ArrayList;
  import java.util.List;
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlType;
  
  
  /**    
   * &lt;p&gt;Java class for root complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="root"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="node" type="{http://xml.netbeans.org/schema/newXmlSchema}node" maxOccurs="unbounded" minOccurs="0"/&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "root", propOrder = {
      "node"
  })
  public class Root {
  
      protected List&lt;Node&gt; node;
  
      /**
       * Gets the value of the node property.
       * 
       * &lt;p&gt;
       * This accessor method returns a reference to the live list,
       * not a snapshot. Therefore any modification you make to the
       * returned list will be present inside the JAXB object.
       * This is why there is not a &lt;CODE&gt;set&lt;/CODE&gt; method for the node property.
       * 
       * &lt;p&gt;
       * For example, to add a new item, do as follows:
       * &lt;pre&gt;
       *    getNode().add(newItem);
       * &lt;/pre&gt;
       * 
       * 
       * &lt;p&gt;
       * Objects of the following type(s) are allowed in the list
       * {@link Node }
       * 
       * 
       */
      public List&lt;Node&gt; getNode() {
          if (node == null) {
              node = new ArrayList&lt;Node&gt;();
          }
          return this.node;
      }
  
  }
  
  
  package org.netbeans.xml.schema.newxmlschema;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlSchemaType;
  import javax.xml.bind.annotation.XmlType;
  import javax.xml.datatype.XMLGregorianCalendar;
  
  
  /**
   * &lt;p&gt;Java class for node complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="node"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
   *         &amp;lt;element name="value" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
   *         &amp;lt;element name="date" type="{http://www.w3.org/2001/XMLSchema}date"/&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "node", propOrder = {
      "name",
      "value",
      "date"
  })
  public class Node {
  
      @XmlElement(required = true)
      protected String name;
      @XmlElement(required = true)
      protected String value;
      @XmlElement(required = true)
      @XmlSchemaType(name = "date")
      protected XMLGregorianCalendar date;
  
      /**
       * Gets the value of the name property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getName() {
          return name;
      }
  
      /**
       * Sets the value of the name property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setName(String value) {
          this.name = value;
      }
  
      /**
       * Gets the value of the value property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getValue() {
          return value;
      }
  
      /**
       * Sets the value of the value property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setValue(String value) {
          this.value = value;
      }
  
      /**
       * Gets the value of the date property.
       * 
       * @return
       *     possible object is
       *     {@link XMLGregorianCalendar }
       *     
       */
      public XMLGregorianCalendar getDate() {
          return date;
      }
  
      /**
       * Sets the value of the date property.
       * 
       * @param value
       *     allowed object is
       *     {@link XMLGregorianCalendar }
       *     
       */
      public void setDate(XMLGregorianCalendar value) {
          this.date = value;
      }
  
  }
  </code></pre>
  
  <p>And WSDL:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;definitions name="newWSDL" targetNamespace="http://j2ee.netbeans.org/wsdl/test/newWSDL"
      xmlns="http://schemas.xmlsoap.org/wsdl/"
      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://j2ee.netbeans.org/wsdl/test/newWSDL" xmlns:ns="http://xml.netbeans.org/schema/newXmlSchema" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype"&gt;
      &lt;types&gt;
          &lt;xsd:schema targetNamespace="http://j2ee.netbeans.org/wsdl/test/newWSDL"&gt;
              &lt;xsd:import namespace="http://xml.netbeans.org/schema/newXmlSchema" schemaLocation="newXmlSchema.xsd"/&gt;
          &lt;/xsd:schema&gt;
      &lt;/types&gt;
      &lt;message name="newWSDLOperationRequest"/&gt;
      &lt;message name="newWSDLOperationResponse"&gt;
          &lt;part name="part1" type="ns:root"/&gt;
      &lt;/message&gt;
      &lt;portType name="newWSDLPortType"&gt;
          &lt;operation name="newWSDLOperation"&gt;
              &lt;input name="input1" message="tns:newWSDLOperationRequest"/&gt;
              &lt;output name="output1" message="tns:newWSDLOperationResponse"/&gt;
          &lt;/operation&gt;
      &lt;/portType&gt;
      &lt;plnk:partnerLinkType name="newWSDL"&gt;
          &lt;plnk:role name="newWSDLPortTypeRole" portType="tns:newWSDLPortType"/&gt;
      &lt;/plnk:partnerLinkType&gt;
  &lt;/definitions&gt;
  </code></pre>
  
  <p>The following soap call:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;S:Header/&gt;
      &lt;S:Body&gt;
          &lt;ns2:newWSDLOperation xmlns:ns2="http://j2ee.netbeans.org/wsdl/test/newWSDL" xmlns:ns3="http://xml.netbeans.org/schema/newXmlSchema"/&gt;
      &lt;/S:Body&gt;
  &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;S:Body&gt;
          &lt;ns3:newWSDLOperationResponse xmlns:ns2="http://xml.netbeans.org/schema/newXmlSchema" xmlns:ns3="http://j2ee.netbeans.org/wsdl/test/newWSDL"&gt;
              &lt;part1/&gt;
          &lt;/ns3:newWSDLOperationResponse&gt;
      &lt;/S:Body&gt;
  &lt;/S:Envelope&gt;
  </code></pre>