Jackson dataformat XML - is it possible to parse element content as a raw string?

Viewed 422

I try to write xml/json parser using Jackson. I use jackson-dataformat-xml that supports XML. I need to extract content of one of XML elements as a raw String and put it as it is into a POJO.

<rootElement>
  <elementNumberOne>...</elementNumberOne>
  <rawElement>rawElementContent<rawElement>
</rootElement>

rawElementContent can be a String or XML structure. Despite what is inside rawElement, the goal is to extract it as a raw value.

I tried to use @JsoneDeserialize and custom Deserializer, but I cannot get the orginial raw xml out of it. The only thing I achieved is to use XmlMapper inside of custom deserializer and use:

mapper.writeValueAsString(jsonNode);

However it's a workaround not a solution (getting Xml to Json and do the opposite way again).

Do you know a better way? Or it's not possible with Jackson?

1 Answers

I reached out to jackson-user group & get response from user Tatu, who confirmed that it's not possible currently. So if you want to stick to Jackson you can go with a workaround I provided in original question (mapper.writeValueAsString).

Tatu encouraged me to filled a ticket, follow if you're interested: https://github.com/FasterXML/jackson-dataformat-xml/issues/417

Related