readValues() from an XML stream

36 views
Skip to first unread message

gapag

unread,
Jan 19, 2022, 5:15:04 PM1/19/22
to jackson-user
Hi everyone,

I would like to read an XML stream with readValues, but if I wish to do something like:

mapper.readValues("<Content><a>i1</a><b>1</b></Content><Content><a>i2</a><b>2</b></Content><Content><a>i3</a><b>3</b></Content>")


Then I get the error:
com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot construct instance of `bet.algotech.persistence.file.Content` (although at least one Creator exists):
no String-argument constructor/factory method to deserialize from String value ('i1')

Note that instead if I provide

mapper.readValues("<Anything><Content><a>i1</a><b>1</b></Content><Content><a>i2</a><b>2</b></Content><Content><a>i3</a><b>3</b></Content></Anything>")

then everything works.
<Anything> can be whatever  - Jackson just needs a start and end tag which does not need to have a corresponding POJO defined in the code.


¶ My question is: Is there a straightforward way to make Jackson work under the assumption that the XML stream contains a sequence of objects of the same type,

-- similarly as it currently happens for the Json case """{"a":"i1", "b":1}{"a":"i2", "b":2}{"a":"i3", "b":2}""" (where no explicit array is given) --

so that this use case does not fail?

Thanks in advance,

-gapag

gapag

unread,
Jan 20, 2022, 11:39:35 AM1/20/22
to jackson-user
Jackson version I am using : 2.13.0

Tatu Saloranta

unread,
Jan 24, 2022, 8:40:23 PM1/24/22
to jackson-user
In theory it is possible to use a sequence, but not exactly like JSON -- the problem being that XML documents' structure requires a single root element (otherwise it's not valid XML document).

But what you would probably need to do would be to construct `FromXmlReader` (subtype of `JsonParser`) first, advance stream to the first XML element of the first value, and then construct `MappingIterator` from `ObjectMapper` (through `ObjectReader`).
I have not tried this approach and I don't think the XML module has unit tests to verify this all works, unfortunately.

You could file an issue showing what you are trying to do, to read a value sequence, and this could lead to verification of this approach, as sort of "new feature" (making sure existing feature works with XML module, content).

- +Tatu +-

 
Thanks in advance,

-gapag

--
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/310be849-73d5-4dbe-aa35-ad38f32642dbn%40googlegroups.com.

gapag

unread,
Jan 25, 2022, 10:51:28 AM1/25/22
to jackson-user
Thanks! Will try and file an issue as soon as I can.

On Tuesday, January 25, 2022 at 2:40:23 AM UTC+1 tsalo wrote:
On Wed, Jan 19, 2022 at 2:15 PM gapag  wrote:
Hi everyone,

I would like to read an XML stream with readValues, but if I wish to do something like:

mapper.readValues("<Content><a>i1</a><b>1</b></Content><Content><a>i2</a><b>2</b></Content><Content><a>i3</a><b>3</b></Content>")



Reply all
Reply to author
Forward
0 new messages