title: Illegal XML/exception un/marshalling shadow variable in JAXB
body: <p>I have the following problem: I have some code (that I cannot change) where one variable of a parent class gets shadowed by a variable of a child class. When annotated as <code>@XmlAttribute</code> and marshalled with JAXB this results in illegal XML and when unmarshalled it results in an exception (due to illegal XML). Here is a minimal example, showing the problem:</p>
  
  <pre><code>    import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.PrintStream;
  
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.Marshaller;
      import javax.xml.bind.Unmarshaller;
      import javax.xml.bind.annotation.XmlAccessType;
      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlAttribute;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.helpers.DefaultValidationEventHandler;
  
      import org.junit.Test;
  
      public class InheritanceJaxbTest {
  
          @Test
          public void testInheritanceField() {
              B b = new B("value");
              String xml = toXML(b);
              System.out.println(xml);
              B b_out = fromXML(xml);
              System.out.println(b_out.myField);
          }
  
          @XmlRootElement
          @XmlAccessorType(XmlAccessType.FIELD)
          private static class A {
  
              public A() {
              }
  
              public A(String myField) {
                  this.myField = myField;
              }
  
              @XmlAttribute
              private String myField;
          }
  
          @XmlRootElement
          @XmlAccessorType(XmlAccessType.FIELD)
          private static class B extends A {
  
              public B() {
                  super();
              }
  
              public B(String myField) {
                  super(myField);
                  this.myField = myField;
              }
  
              @XmlAttribute
              private String myField;
          }
  
          public &lt;T&gt; T fromXML(String xml) {
              try {
                  JAXBContext jc = JAXBContext.newInstance(A.class, B.class);
                  Unmarshaller unmarshaller = jc.createUnmarshaller();
                  unmarshaller.setEventHandler(new DefaultValidationEventHandler());
                  return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));
              } catch (Exception exc) {
                  throw new RuntimeException(exc);
              }
          }
  
          public String toXML(Object obj) {
              try {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  PrintStream ps = new PrintStream(baos);
                  JAXBContext jc = JAXBContext.newInstance(A.class, B.class);
                  Marshaller marshaller = jc.createMarshaller();
                  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                  marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
                  marshaller.setEventHandler(new DefaultValidationEventHandler());
                  marshaller.marshal(obj, ps);
                  return baos.toString();
              } catch (Exception exc) {
                  throw new RuntimeException(exc);
              }
          }
      }
  </code></pre>
  
  <p>This produces the following (obviously illegal) XML:</p>
  
  <pre><code>&lt;b myField="value" myField="value"/&gt;
  </code></pre>
  
  <p>And the subsequent unmarshalling operation throws the following exception:</p>
  
  <pre><code>java.lang.RuntimeException: javax.xml.bind.UnmarshalException
  - with linked exception:
  [org.xml.sax.SAXParseException: Attribute "myField" was already specified for element "b".]
  at InheritanceJaxbTest.fromXML(InheritanceJaxbTest.java:67)
  </code></pre>
  
  <p>Since I basically cannot change the underlying Java classes, I want to solve this problem with some kind of XmlAdapter or custom XmlStreamWriter or something similar. Any suggestions on how to proceed?</p>
  
  <p><a href="https://stackoverflow.com/questions/19467116/unable-to-unmarhsal-shadow-class-variable-in-jaxb">This question</a> is related but gives nonetheless no insights how to proceed without changing the Java classes.</p>