Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JacksonXmlText cannot retrieve a wrapped text from a valid XML #574

Closed
forceporquillo opened this issue Feb 23, 2023 · 3 comments
Closed

Comments

@forceporquillo
Copy link

forceporquillo commented Feb 23, 2023

Given a POJO, I should be able to deserialize any element value from an XML element.

suppose, this object:

@Getter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class MatchingGroups {

   ...

   @JacksonXmlText
   private String content;
}

When deserializing a value from an XML element, note: that the value from that element is wrapped inside an XML element.

<MatchingGroups1 id="123" format="foo"><test>1</test></MatchingGroups1>

The expected value of matchingGroups.getContent() should be <test>1</test> however, in my case it was empty.

I am aware that the docs say:
Allows specifying that value of one property is to be serialized as "unwrapped" text, and not in an element.

I am using the 2.13.4 version. Is there a way how we can easily achieve retrieving a wrapped text from an XML element?

@forceporquillo forceporquillo changed the title @JacksonXmlText cannot deserialize a valid XML value @JacksonXmlText cannot retrieve a wrapped text from a valid XML Feb 23, 2023
@cowtowncoder
Copy link
Member

No, that's not how XML Text works -- you cannot arbitrarily claim to not parse XML elements and leave them as "general XML content". If there is element <test>, it must match structure; XML parser will (and must) decode it separately into elements, text segments and so on.

Put another way, @JacksonXmlText can only map XML text segments (text within element), not for XML sub-trees.

You could bind it into

 private Object content;

or

private JsonNode content;

in which case contents would be read into Maps or JsonNode.

@forceporquillo
Copy link
Author

Thanks for your clarifications and insights! I was able to resolve it by following your suggestion and reading the contents via JsonNode. Closing this issue.

@cowtowncoder
Copy link
Member

@forceporquillo I am glad work-around works for you -- good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants