On Wed, Jan 19, 2022 at 2:15 PM gapag <[email protected]> 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>")*
>
>
> *Find a concrete example in *
> *https://gist.github.com/gapag/8856983e48d94b57c5c63c46dc477cbc*
> <https://gist.github.com/gapag/8856983e48d94b57c5c63c46dc477cbc>
>
> 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?
>
>
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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jackson-user/310be849-73d5-4dbe-aa35-ad38f32642dbn%40googlegroups.com
> <https://groups.google.com/d/msgid/jackson-user/310be849-73d5-4dbe-aa35-ad38f32642dbn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/CAGrxA24x85v2sVSBsPbQTp-gsrt7JpYZieuyYOU57vPBeXJz%3DQ%40mail.gmail.com.