Using the the IS_REPLACING_ENTITY_REFERENCES
with a StAX parser should give you the behaviour that you are looking for,
package forum13235119;
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/forum13235119/input.xml"));
Unmarshaller unmarshaller = jc.createUnmarshaller();
Foo foo = (Foo) unmarshaller.unmarshal(xsr);
System.out.println(foo.bar);
}
}
Note:
This isn't working in my environment, but does work for some StAX parsers based on the following answer to a similar question: