3

I'm trying to convert an object to xml using jackson

XmlMapper xmlMapper = new XmlMapper();
            xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
            xmlMapper.writeValue(createFileResult.getFile(), detailsProductDTOs);

however, because my object somewhere contains invalid character for xml, I get an error. Is there some way to tell jackson to ignore this kind of characters? or any other solution?

Caused by: java.io.IOException: Invalid white space character (0xb) in text to output (in xml 1.1, could output as a character entity)
    at com.ctc.wstx.api.InvalidCharHandler$FailingHandler.convertInvalidChar(InvalidCharHandler.java:56)
    at com.ctc.wstx.sw.XmlWriter.handleInvalidChar(XmlWriter.java:629)
    at com.ctc.wstx.sw.BufferingXmlWriter.writeCharacters(BufferingXmlWriter.java:583)
    at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:469)
    at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeString(ToXmlGenerator.java:588)
Luka Gorgadze
  • 131
  • 1
  • 7
  • please share the code related with object and it's data – Lakshan Apr 18 '19 at 15:29
  • Could you just remove the invalid characters from the strings in your DTOs using [removing invalid XML characters from a string in java](https://stackoverflow.com/q/4237625) or [Stripping Invalid XML characters in Java](https://stackoverflow.com/q/93655) or [Filtering illegal XML characters in Java](https://stackoverflow.com/q/2897085)? – dbc Apr 18 '19 at 18:57
  • dbc , I can't solve my problem in this way because its very complex object, it will be very ugly and buggy code if I'll try to check every string that it contains – Luka Gorgadze Apr 19 '19 at 06:58

1 Answers1

2

Finally, I solved the problem by myself. As error indicates xml 1.1 supports this kind of characters. So its possible to solve the problem just by adding following configuration:

xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
Luka Gorgadze
  • 131
  • 1
  • 7