javaxmljaxbconverters

Convert Xml to java object ignoring meta symbols


I tried to convert xml to java object. All xmls convert great but if one field has value with & I get exception `jakarta.xml.bind.UnmarshalException

Short xml

<?xml version="1.0" encoding="UTF-8"?> 
<SomeResponse> 
<Some>Kondor#C&F#1435</Some> 
</SomeResponse>

As you see, my value contains # and & symbols. How can I convert it like just string?

        JAXBContext context = JAXBContext.newInstance(className);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        StringReader reader = new StringReader(message);
        Object unmarshalled = unmarshaller.unmarshal(reader);

Thanks!

I saw into the library, tried XmlMapper and etc but all of these have the same problem


Solution

  • You can't parse this "XML" because it not valid XML.

    You only option is seems to be is to create your own parser.

    If you can change source of "XML" you should put your content inside CDATA like this <Some><![CDATA[Kondor#C&F#1435]]></Some>.