Exclude empty Arrays and values from Jackson XmlMapper

27 Views Asked by At

I am trying to form an Xml from a json. I have Object templates for json and using ObjectMapper to map. While i transfrom, there are some aspects that i need to follow for required rules. If an array or object is null i shouldn't be writing it on Xml. This "JsonInclude.Include.NON_EMPTY" tag should have solve it but it doesn't.

Additionally i need to change the encoding to "ISO-8859-1".

jsonStr is a json string

ObjectMapper objectMapper = new ObjectMapper();
            com.main.models.MyObject.Obj1 
jsonNode = objectMapper.readValue(jsonStr, com.main.models.MyObject.Obj1.class);

            XmlMapper mapper = new XmlMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);

            ObjectWriter writer = mapper.writer().withRootName("root");

            String xmlString = writer.writeValueAsString(jsonNode);
0

There are 0 best solutions below