Search code examples
javajackson-dataformat-xml

Jackson generates xml without the <?xml?> tag at the top


I have a Java app that uses Jackson to generate a xml, but the final xml doesn't have the <?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> at the beginig of the document, it starts at the <source> tag.

I need this tag to appear at the top, but I can't manage to do so.


Solution

  • Assuming you are using XmlMapper you can add 'WRITE_XML_DECLARATION' configuration to add it

        XmlMapper xmlMapper =  new XmlMapper();
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);//XML declaration
    

    See, this question - Easy way to add DOCTYPE to XML with Jackson