Q:

JAXB unmarshall Mixed Content

I'm trying to parse existing XML files for a java application and some of the elements have mixed content occasionally as demonstrated below:
XML_STRING
<root>
  <name>Michael</name>
  <question>Text here</question>
</root>

XML_STRING_2
<root>
  <name>Michael</name>
  <question>Text here<measure>More Text</measure></question>
</root>

I Made the below classes to unmarshall this data.
Root Class
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root implements Serializable
{
    private String name;
    private Question question;
}

Question Class
@XmlAccessorType(XmlAccessType.NONE)
public class Question implements Serializable
{
    @XmlValue
    private String questionText;
    private String measure;
}

I can't seem to get both the Text Here and More Text to be stored inside the Question class.
JAXBContext.newInstance(Root.class)
  .createUnmarshaller()
  .unmarshal(new ByteArrayInputStream(XML_STRING_2.getBytes("UTF-8")));

I'm printing the result of the above code snippet (with Lombok's @ToString() annotation added to Root and Question) for both XML_STRING and XML_STRING_2.
XML_STRING: Root(name=Michael, question=Question(questionText=Text here, measure=null))
XML_STRING_2:Root(name=Michael, question=Question(questionText=, measure=null))

A:

I was able to get something I could work with by using @XmlMixed along with @XmlElementRefs inside Question and creating a Measure class.
@XmlAccessorType(XmlAccessType.NONE)
public class Question implements Serializable
{
    @XmlMixed
    @XmlElementRefs({
        @XmlElementRef(name = "measure", type=Measure.class)
    })
    private List<?> content;
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="measure") // If this is removed I get the error: "Invalid @XmlElementRef : Type `Measure` or any of its subclasses are not known to this context."
public class Measure
{
    @XmlValue
    private String value;
}

I'm now getting the below output, which i can use via instanceof checking the items inside content
Root(name=Michael, question=Question(content=[Text here]))
Root(name=Michael, question=Question(content=[Text here, Measure(value=More Text)]))

The Root file remained unchanged.

