I have XML like this:
....
<dateIssuedField class="org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl" resolves-to="org.apache.xerces.jaxp.datatype.SerializedXMLGregorianCalendar">
<lexicalValue>0001-01-01T00:00:00</lexicalValue>
</dateIssuedField>
....
and I'd like to get JSON like this:
"dateIssuedField": {"0001-01-01T00:00:00"}
I've tried so many things. Unfortunately this 'class' and 'resolves-to' don't actually match any existing class. So I can't add a serializer/deserializer that matches these classes.
The simplest fix would be to get rid of all attributes. Then I'll just deal with 'lexicalValue'. But I can't even work that out.
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode node = xmlMapper.readTree(xml.getBytes());
ObjectMapper objectMapper = new ObjectMapper();
String jsonOutput = objectMapper.writeValueAsString(node);
I don't have the classes generated, so I can't use annotations to ignore the fields.
It looks like there is a MixIn technique, but I can't work out how to use it in this case.
As I commented: The desired output format
is not really/fully valid JSON...
(1) null value:
or (2) single element array:
or just (my fav 3):
would be.
With
in.xml:(jdk:19, jackson-databind+jackson-dataformat-xml:2.14.2), we can navigate and modify the object tree as see fit.
To achieve (1):
prints:
For (my fav 3) we can:
prints:
Even better: Map your model (of interest) as objects!
For larger models, it is worth to cope with (model/object/schema) "generation". And always an option: To model "manually".
Advantages: