1

I have a Map key/value: string/string which contains an XML element name as key and a large XML nested string as value, e.g:

Map<String, String> obj;

INSPIRE => <oi-cov:OrthoimageCoverageMetadata xmlns:oi-cov="http://inspire.ec.europa.eu/schemas/oi/4.0" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:base="http://inspire.ec.europa.eu/schemas/base/3.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://inspire.ec.europa.eu/schemas/oi/4.0 https://schema.datacove.eu/OrthoimageryMetadata.xsd"> <oi-cov:inspireId> ...

Using Jackson to serialize this object as String, the XML special character '<' and '>' in the map value always translated to '&lt;' and '&gt;'. How can I prevent it?

private static final XmlMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writer().writeValueAsString(obj);

The current output as string is:

<INSPIRE>&lt;oi-cov:OrthoimageCoverageMetadata xmlns:oi-cov="http://inspire.ec.europa.eu/schemas/oi/4.0" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:base="http://inspire.ec.europa.eu/schemas/base/3.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://inspire.ec.europa.eu/schemas/oi/4.0 https://schema.datacove.eu/OrthoimageryMetadata.xsd"&gt;
&lt;oi-cov:inspireId&gt;
    &lt;base:Identifier&gt;
Roger Gustavsson
  • 1,689
  • 10
  • 20
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
  • Well you are serializing to XML, even if you're only getting the string representation, so it makes a lot of sense for special characters to be escaped - otherwise your resulting markup would very likely be broken. Have you considered getting rid of the property value "as string" and instead have a nested POJO representing that value, so it can be serialized within the parent POJO? – Mena Oct 09 '19 at 10:38
  • @Mena the problem is the XML value here is pretty much a general XML with arbitrary nested XML elements so I cannot map it as a POJO object. – Bằng Rikimaru Oct 09 '19 at 10:46
  • Understood. But then what is your expectation on how it should be serialized? If the special characters weren't escaped, you would end up with invalid XML - in fact you probably wouldn't even be able to de-serialize back into the pojo. My suspicion is that he issue is at the root, i.e. why did you end up with a map of String -> arbitrary markup and can you escape that design trap? – Mena Oct 09 '19 at 11:13
  • For instance, if those arbitrary markup values have some common structure, you could try and create a POJO hierarchy reflecting it and use annotation for polymorphic collections so they are de-serialized to the correct child POJO on the way back (assuming that's in the plans). – Mena Oct 09 '19 at 11:15
  • 2
    Since Jackson's `XmlMapper` claims to follow JAXB, then perhaps the answer to this question will help you: [JAXB - marshal java object with XML string field](https://stackoverflow.com/q/14777444/5221149) – Andreas Oct 09 '19 at 11:20

0 Answers0