title: Use Jaxb to unmarshall part of XML to an instance of org.jdom.Element
body: <p>I'm working in a legacy product that heavily relies on version 1 of the org.jdom project (<a href="http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3" rel="nofollow">http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3</a>) and manually constructed XMLs but I would like to use Jaxb as much as possible instead. We're using Moxy as the Jaxb implementation.</p>
  
  <p>So say that I have the following xml:</p>
  
  <pre><code>&lt;foo bar="bar"&gt;
      &lt;baz&gt;
          &lt;test value="something" /&gt;
      &lt;/baz&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>And because of legacy code using the baz element as an org.jdom.Element I would like to have Jaxb unmarshall the inner element "baz" to an org.jdom.Element but v in unmarshal is always an empty string ("") so baz becomes null.
  I've created an example below where I try to use an XmlAdapter but I can't get it to work.</p>
  
  <pre><code>import java.io.ByteArrayInputStream;
  import java.io.StringReader;
  import java.nio.charset.StandardCharsets;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  import javax.xml.transform.stream.StreamSource;
  
  import org.jdom.Document;
  import org.jdom.Element;
  import org.jdom.input.SAXBuilder;
  import org.jdom.output.XMLOutputter;
  
  public class Test {
  
    public static void main(String[] args) throws JAXBException {
  
      String fooString = "&lt;foo bar=\"bar\"&gt;&lt;baz&gt;&lt;test value=\"something\" /&gt;&lt;/baz&gt;&lt;/foo&gt;";
      JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
              .createContext(new Class&lt;?&gt;[] {Foo.class}, null);
      javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      Foo foo = unmarshaller
              .unmarshal(new StreamSource(new ByteArrayInputStream(fooString.getBytes(StandardCharsets.UTF_8))), Foo.class)
              .getValue();
  
      System.out.println(foo);
    }
  
    @XmlRootElement
    private static final class Foo {
  
      @XmlAttribute
      public String bar;
  
      @XmlElement
      @XmlJavaTypeAdapter(ElementAdapter.class)
      public Element baz;
  
      @Override
      public String toString() {
          return "Foo [bar=" + bar + ", baz=" + baz + "]";
      }
  
    }
  
    private static final class ElementAdapter extends XmlAdapter&lt;String, Element&gt; {
  
      @Override
      public Element unmarshal(String v) throws Exception {
          Document document = new SAXBuilder().build(new StringReader(v));
          return document.getRootElement();
      }
  
      @Override
      public String marshal(Element v) throws Exception {
          return new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).outputString(v);
      }
  
    }
  
  }
  </code></pre>
  
  <p>Maybe I'm attacking this from the wrong angle. Any suggestions on how to achieve this?</p>