-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
XML attributes are not correctly populated when converting from XML (ObjectNode) to String using writeValueAsString (XML structure) #613
Comments
I am also observing the same behavior , all my attributes are converted to sub elements |
FasterXML/jackson-module-jaxb-annotations#27 This should help you if not tried already |
Some quick notes:
It almost feels like a XML-specific subset of There are other possible feature additions that could help: for example, use of naming convention or wrappers could allow something like prefixing names of attributes with |
Hi @cowtowncoder,
We are planning to migrate our old deserialization logic from using fragmented substrings between
<open>
and</closing>
tags when deserializing an XML String to POJOs into Jackson’s sophisticated customStdDeserializers/JsonDeserializers
directly starting from version2.15.2
.I have a few questions about whether this feature of mine already exists or not.
My team and I are trying to deserialize a very complex XML structure, to the point that we do not have to create multiple beans because some of the content of our XML subtree often changes dynamically with minimal format structure (we have
~10 XML formats
to support with or without attributes and namespaces) and is also a valid XML structure. WE mostly used custom deserializers likeStdDeserializer
andJsonDeserializer
to mapObjectNode
into plainMap
(well internally, this is already aMap
) (thanks for your suggestion in #574) and then later converted it back to String.While I still stumbled across our implementation to keep our XML subtree to be deserialized via substring. But then later we planned to stick to using
JsonNode
to retrieve segments of XML and convert it back to the original XML as String usingSegmentedStringWriter#writeValueAsString
, for a reason that we wanted to leverage thejava.xml.stream.*
without the need to retrieve full string token and to fully adaptJackson's Streaming API
interoperability. We do not want to convert it into a plain POJO, but instead, would rather use thejava.util.Map
class to keep it generic and to store everyJsonNode
token retrieved fromObjectNode#getValue()
.Sample Scenario
Supposedly, I have an XML element that contains an attribute
name
andage
.XML Attribute
The output I would expect should be the same as the original XML, but in my case, it's not:
The attributes became sub-elements of the original element, while the element tag of the original element value becomes empty:
<>...</>
XML Namespace
The same happens with namespaces:
suppose Animal namespace declaration:
<ns1:animal xmlns:ns1="urn:this:is:namespace:for:animal">
Output:
The namespace becomes a new element.
My assumption is that it will only work with a plain XML structure (excluding the attributes itself) or with bean-defined POJOs annotated XML attr properties.
Sampe Driver Code
Tried using custom XmlMapper config:
and with a custom deserializer for the
Animal
bean.AnimalDeserializer.java
Update:
When using
TypeReference<Map<String, String>(){}
, it prints the expected XML output. However, the attribute is still missing.Output:
We really appreciate your feedback and suggestions for this @cowtowncoder
The text was updated successfully, but these errors were encountered: