title: JAXB XmlAdapter via bindings file
body: <p>Consider this XSD:</p>
  
  <pre><code>&lt;xsd:schema targetNamespace="http://foobar" xmlns:tns="http://foobar"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  
      &lt;xsd:complexType name="IdAttribute"&gt;
          &lt;xsd:attribute name="id" type="xsd:token" /&gt;
      &lt;/xsd:complexType&gt;
  
      &lt;xsd:complexType name="FoobarType"&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="someIds" type="tns:IdAttribute" maxOccurs="unbounded" /&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
  
      &lt;xsd:element name="Foobar" type="tns:FoobarType" /&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>which results in the following, generated Java class:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "FoobarType", propOrder = {"someIds"})
  public class FoobarType {
  
      @XmlElement(required = true)
      protected List&lt;IdAttribute&gt; someIds;
  
      // ...
  }
  </code></pre>
  
  <hr>
  
  <p>Because <code>IdAttribute</code> only contains one String (<code>id</code>), I want to map these Strings directly into my <code>FoobarType</code> for easier usage. Therefore I wrote an <code>XmlAdapter</code>:</p>
  
  <pre><code>public class IdAttributeAdapter extends XmlAdapter&lt;IdAttribute, String&gt; { ... }
  </code></pre>
  
  <p>I've edited the generated class manually to verify my <code>XmlAdapter</code> works as expected (it does):</p>
  
  <pre><code>@XmlElement(required = true)
  @XmlJavaTypeAdapter(IdAttributeAdapter.class)
  protected List&lt;String&gt; someIds;
  </code></pre>
  
  <hr>
  
  <p>Of course I want to have this little code change done during code generation, so I've added the following bindings file (<code>src/main/xjb/bindings.xjb</code>):</p>
  
  <h1>Attempt 1:</h1>
  
  <pre><code>&lt;jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"&gt;
  
      &lt;jaxb:bindings schemaLocation="../resources/xsd/foobar.xsd"&gt;
          &lt;jaxb:bindings node="//xsd:complexType[@name='IdAttribute']"&gt;
              &lt;xjc:javaType
                  name="java.lang.String"
                  adapter="org.foobar.IdAttributeAdapter" /&gt;
          &lt;/jaxb:bindings&gt;
      &lt;/jaxb:bindings&gt;
  
  &lt;/jaxb:bindings&gt;
  </code></pre>
  
  <h2>Result</h2>
  
  <pre><code>com.sun.istack.SAXParseException2; systemId: file:/C:/dev/foobar/src/main/xjb/bindings.xjb;
  lineNumber: 12; columnNumber: 91;
  compiler was unable to honor this conversion customization. It is attached to a wrong place, or its inconsistent with other bindings.
  </code></pre>
  
  <h1>Attempt 2 (from <a href="https://stackoverflow.com/questions/7837451/specify-xmljavatypeadapter-class-via-bindings-file">here</a>):</h1>
  
  <pre><code>&lt;jaxb:globalBindings xmlns:foo="http://foobar" xsi:schemaLocation="http://foobar ../resources/xsd/foobar.xsd"&gt;
      &lt;xjc:javaType
          name="java.lang.String"
          xmlType="foo:IdAttribute"
          adapter="org.foobar.IdAttributeAdapter" /&gt;
  &lt;/jaxb:globalBindings&gt;
  </code></pre>
  
  <h2>Result</h2>
  
  <pre><code>com.sun.istack.SAXParseException2; systemId: file:/C:/dev/projects/luna/oasch/src/main/xjb/bindings.xjb;
  lineNumber: 8; columnNumber: 112;
  undefined simple type "{http://foobar}IdAttribute".
  </code></pre>
  
  <p>I've tried a few variations, but they all resulted in similar errors. So what's the correct way to add an <code>XmlApender</code> using a bindings file?</p>