3

I have an incoming XML with a value which is an encoded form such as &ltimg src=/".../" &gt , but when unmarshalling this data JAXB is decoding the data to <img src=/"../" > which I do not want it to do.

Is there any configuration to disable this behavior in JAXB?

Echilon
  • 10,064
  • 33
  • 131
  • 217

2 Answers2

1

Define your variable value as CDATA by using annotations or otherwise

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
0

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:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400