title: JAXB: unmarshal from XML subtree?
body: <p>My JAVA program uses an internal class hierarchy which resembles GPX 1.1, but is not identical. since rewriting it to conform 1:1 to GPX is a huge effort, I'd like to change it bit by bit, i.e. reading the <code>&lt;metadata&gt;</code> subtree into the Class <code>MetadataType</code> as generated with xjc from <a href="http://www.topografix.com/GPX/1/1/gpx.xsd" rel="nofollow">the XSD file</a></p>
  
  <p>the remaining GPX file is parsed with DOM, until <code>&lt;metadata&gt;</code> shows up:</p>
  
  <pre><code>private void parseMetadata(MetadataType metadata, Element element) throws JAXBException {
      try {
          System.out.println(element.getNodeName()); // output: metadata
          JAXBContext context = JAXBContext.newInstance(MetadataType.class);
  
          javax.xml.bind.Unmarshaller u = context.createUnmarshaller();
  
          JAXBElement&lt;MetadataType&gt; meta = u.unmarshal(element, MetadataType.class);
          metadata = meta.getValue();
  
          } catch (Exception e) { 
              e.printStackTrace();
          }
      System.out.println(metadata.getName()); // NULL
      System.out.println(metadata.getAuthor().getName()); // NULL
  }
  </code></pre>
  
  <p>throws
  <code>javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"metadata"). Expected elements are &lt;{http://www.topografix.com/GPX/1/1}gpx&gt;
      at  com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)</code></p>
  
  <p>the class looks like this:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "metadataType", propOrder = {
  "name",
  "desc",
  "author",
  "copyright",
  "link",
  "time",
  "keywords",
  "bounds",
  "extensions"
  })
  @XmlRootElement(name = "metadata")
  public class MetadataType {
  
  protected String name = "";
  protected String desc = "";
  protected PersonType author;
  protected CopyrightType copyright = new CopyrightType();
  protected List&lt;LinkType&gt; link = new ArrayList&lt;LinkType&gt;();
  @XmlSchemaType(name = "Date")
  protected Date time;
  protected String keywords = "";
  protected BoundsType bounds = new BoundsType();
  protected ExtensionsType extensions = new ExtensionsType();
  [...]
  }
  </code></pre>
  
  <p>is it possible to unmarshal from an XML subtree; and if yes, what am I doing wrong?</p>
  
  <p>UPDATE/1: 
  thanks to lexicore, I'm a step further: <code>element</code> definitely contains the <code>metadata</code> node, <code>@XmlRootElement</code> is set, unmarshaling now with <code>unmarshal(element, MetadataType.class)</code>.</p>
  
  <p>unmarshaling works, but the content of the objects is empty. I wonder if I run into some namespace troubles here?</p>
  
  <p>the package <code>com.topografix.gpx._1._1</code> contains a package-info.java, generated by xjc: </p>
  
  <pre><code>@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.topografix.com/GPX/1/1", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
  package com.topografix.gpx._1._1;
  </code></pre>
  
  <p>here's one of the test files:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt;
  &lt;gpx xmlns="http://www.topografix.com/GPX/1/1" 
  xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" 
  xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1" 
  xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" 
  creator="GPSMAP 62s" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.topografix.com/GPX/1/1 
  http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas /GpxExtensions/v3 
  http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 
  http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 
  http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"&gt;
  &lt;metadata&gt;
  &lt;link href="http://www.garmin.com"&gt;
  &lt;text&gt;Garmin International&lt;/text&gt;
  &lt;/link&gt;
  &lt;time&gt;2014-01-01T22:26:49Z&lt;/time&gt;
  &lt;/metadata&gt;
  [...]
  </code></pre>
  
  <p>UPDATE/2: the namespace/package name is right. If the namespace is set to (i.e.) "foobar", the following exception is thrown:
  <code>javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated: 
  javax.xml.bind.JAXBException: "foobar" doesnt contain ObjectFactory.class or jaxb.index</code></p>
  
  <p>when initialized as before, the exception is not thrown, meaning that the namespace is correct AND the ObjectFactory.class is found.</p>
  
  <p><code>String contextPath = MetadataType.class.getPackage().getName();
      JAXBContext context = JAXBContext.newInstance(contextPath);</code></p>
  
  <p>So, the namespace is correct, but somehow the "link" to the MetadataType class is missing?</p>