Configuring JAXB unmarshal tp avoid decoding the XML data

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?

Upvotes: 3

Views: 838

Answers (2)

bdoughan
bdoughan

Reputation: 149047

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:

Upvotes: 0

Germann Arlington
Germann Arlington

Reputation: 3353

Define your variable value as CDATA by using annotations or otherwise

Upvotes: 1

Related Questions