JAX-WS (JAXB) and unmarshalling mixed xs:anyType
<p>I have a web service I have to call using a proxy generated using <code>xcj</code> (for <code>.xsd</code>) and <code>wsimport</code> (for <code>.wsdl</code>) definitions. It is external (i.e. cannot edit it), huge and ever-changing (cannot hand-tweak generated classes).</p>

<p>It has a superclass-like <code>xs:anyType</code> element with <code>mixed="true"</code>, similar to sample below.</p>

<pre><code>&lt;Foo type="Bar"&gt;
    &lt;id&gt;123&lt;/id&gt;
&lt;/Foo&gt;

&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="Foo"&gt;
        &lt;xs:complexType&gt;
            &lt;xs:complexContent mixed="true"&gt;
                &lt;xs:extension base="xs:anyType"&gt;
                    &lt;xs:attribute name="type" type="xs:string"/&gt;
                &lt;/xs:extension&gt;
            &lt;/xs:complexContent&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;

    &lt;xs:element name="Bar"&gt;
        &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
                &lt;xs:element name="id" type="xs:int"/&gt;
                &lt;xs:element name="name" type="xs:string"/&gt;
            &lt;/xs:sequence&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
&lt;/xs:schema&gt;
</code></pre>

<p><code>xjc</code>/<code>wsimport</code> generates <code>Foo</code> as follows</p>

<pre><code>@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "content" })
@XmlRootElement(name = "Foo")
public class Foo {
    @XmlMixed
    @XmlAnyElement
    protected List&lt;Object&gt; content;
    @XmlAttribute(name = "type")
    protected String type;
    @XmlAnyAttribute
    private Map&lt;QName, String&gt; otherAttributes = new HashMap&lt;QName, String&gt;();
</code></pre>

<p>I can marshall a Foo object to its <em>root-less</em> form using <code>Document.getChildNodes()</code> (although I wouldn't mind finding a cleaner way), but I cannot figure out how to unmarshal response's <code>content</code> list (which is <code>ArrayList&lt;ElementNSImpl&gt;</code> after proxy is done with it) to strongly-typed <code>Bar</code>?</p>

<pre><code>public static void main(String[] args) throws Exception {
    Foo foo = new Foo();
    foo.type = "Bar";

    Bar bar = new Bar();
    bar.id = 123;

    JAXBContext context = JAXBContext.newInstance(Foo.class, Bar.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    marshaller.marshal(bar, document);

    NodeList nodes = document.getFirstChild().getChildNodes();
    foo.content = IntStream.range(0, nodes.getLength()).mapToObj(nodes::item).collect(Collectors.toList());

    StringWriter writer = new StringWriter();
    marshaller.marshal(foo, writer);
    System.out.println(writer);

    Unmarshaller unmarshaller = context.createUnmarshaller();

    // ???
    unmarshaller.unmarshal(foo.content, Bar.class);
}
</code></pre>