Deserialize whole XML element as a raw String.

57 views
Skip to first unread message

Łukasz Gebel

unread,
Jul 10, 2020, 11:29:12 PM7/10/20
to jackson-user
Hi!

I try to achieve the following thing using Jackson-data-format-xml:
<rootElement><firstElement>value</firstElement><rawElement>...</rawElement><rootElement>

And after deserializing it I need to read the whole rawElement as well as its children as String property (without any changes).
I tried to use @JsonDeserialize & custom deserializer, but it works on JsonNode abstraction & I cannot get to the raw XML values.

Thanks in advance.

Tatu Saloranta

unread,
Jul 10, 2020, 11:57:34 PM7/10/20
to jackson-user
There is no way to get underlying "raw" XML content in any way: most
Java parsers typically do not expose it (as they do not necessarily
buffer undecoded content but handle entity replacement, linefeed
normalization as so on).

-+ Tatu +-

Łukasz Gebel

unread,
Jul 11, 2020, 4:37:02 AM7/11/20
to jackso...@googlegroups.com
Thanks Tatu,

it means I need to use e.g Jaxb or other tool that operates on Xml for xml parsing, and get elemnts I need.

I wanted to have one code for formats, and it would work in jackson if not this edge case.

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10iFYDvwabMqKauzNOzBNpWAnj255h0qhYBVucNtHW%2Bs%3Dg%40mail.gmail.com.

Tatu Saloranta

unread,
Jul 11, 2020, 6:47:53 PM7/11/20
to jackson-user
On Sat, Jul 11, 2020 at 1:37 AM Łukasz Gebel <luk....@gmail.com> wrote:
>
> Thanks Tatu,
>
> it means I need to use e.g Jaxb or other tool that operates on Xml for xml parsing, and get elemnts I need.
>
> I wanted to have one code for formats, and it would work in jackson if not this edge case.

If we are talking about "rawest" XML content, JAXB would not expose it
to you either (content you see is pre-processed
to normalize white-space, entities), but I am guessing you are talking
about access to content as DOM, which is somewhat
cleaned up, but has full XML Infoset contents available.
While that is something Jackson could theoretically support -- I
actually thought a bit about this wrt 2.12 -- it is not currently
supported.

Jackson 2.12 actually adds (finally!) proper support for `JsonNode` on
XML, so that duplicate properties will be retained,
and character data segments too (formerly either elements or cdata
retained, not both). This will get you little bit closer,
but not quite there, since the full ordering is not retained (that is,
cdata is under key "", empty String, under first location
it was seen).

Now... to get further, it'd be necessary to expose a "native" XML
type, like `org.w3c.dom.Node` (or `Element`?).
Currently binding would try to re-parse textual content, but it would
be relatively easy to change that, possibly along
with configuration setting to select new or old approach.

If this makes sense, would you like to file an issue for such a feature?

-+ Tatu +-

>
> sob., 11 lip 2020, 05:57 użytkownik Tatu Saloranta <ta...@fasterxml.com> napisał:
>>
>> On Fri, Jul 10, 2020 at 8:29 PM Łukasz Gebel <luk....@gmail.com> wrote:
>> >
>> > Hi!
>> >
>> > I try to achieve the following thing using Jackson-data-format-xml:
>> > <rootElement><firstElement>value</firstElement><rawElement>...</rawElement><rootElement>
>> >
>> > And after deserializing it I need to read the whole rawElement as well as its children as String property (without any changes).
>> > I tried to use @JsonDeserialize & custom deserializer, but it works on JsonNode abstraction & I cannot get to the raw XML values.
>>
>> There is no way to get underlying "raw" XML content in any way: most
>> Java parsers typically do not expose it (as they do not necessarily
>> buffer undecoded content but handle entity replacement, linefeed
>> normalization as so on).
>>
>> -+ Tatu +-
>>
>> --
>> You received this message because you are subscribed to the Google Groups "jackson-user" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10iFYDvwabMqKauzNOzBNpWAnj255h0qhYBVucNtHW%2Bs%3Dg%40mail.gmail.com.
>
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAF-mXfm-94BdhCLAfXCYuQZNijRAbqmSBdPBAY_UR56jTt25PQ%40mail.gmail.com.

Łukasz Gebel

unread,
Jul 13, 2020, 3:39:03 PM7/13/20
to jackso...@googlegroups.com
Thanks Tatu!

Sure, I'll fill the issue this week.

Best regards,
Luk

Łukasz Gebel

unread,
Jul 16, 2020, 1:38:55 PM7/16/20
to jackso...@googlegroups.com
Hi Tatu,


Kindest regards,
Luk

Tatu Saloranta

unread,
Jul 17, 2020, 12:17:01 AM7/17/20
to jackson-user
Reply all
Reply to author
Forward
0 new messages