I need to serialize an XML, the input I want is something like:
<item id="123">description</item>
I have a serializer that has the following code:
toXmlGenerator.writeFieldName("item");
toXmlGenerator.writeStartObject();
toXmlGenerator.setNextIsAttribute(true);
toXmlGenerator.writeFieldName("id");
toXmlGenerator.writeString(element.getId());
toXmlGenerator.setNextIsAttribute(false);
toXmlGenerator.writeStringField("",element.getValue());
toXmlGenerator.writeEndObject();
with the above I get :
<item id="123">
<>description</>
</item>
I've tried different options with toXmlGenerator.writeStringField("",element.getValue());
but nothing works, I tried toXmlGenerator.writeString(element.getValue());
but I get the following error:
com.fasterxml.jackson.databind.JsonMappingException: Can not write String value, expecting field name
Is there a way to do this I am missing?
There is a setNextIsUnwrapped(boolean)
method that enables a value output without the node name.
toXmlGenerator.setNextIsUnwrapped(true);
It should be placed before writeStringField()
:
toXmlGenerator.setNextIsAttribute(false);
toXmlGenerator.setNextIsUnwrapped(true);
toXmlGenerator.writeStringField("", element.getValue());
toXmlGenerator.writeEndObject();