xmlgroovygpath

GPathResult to String without XML declaration


I'm converting GPathResult to String using

def gPathResult = new XmlSlurper().parseText('<node/>')
XmlUtil.serialize(gPathResult)

It works fine, but I'm getting XML declaration in front of my XML

<?xml version="1.0" encoding="UTF-8"?><node/>

How can I convert GPathResult to String without <?xml version="1.0" encoding="UTF-8"?> at the beginning?


Solution

  • Use XmlParser instead of XmlSlurper:

    def root = new XmlParser().parseText('<node/>')
    new XmlNodePrinter().print(root)
    

    Using new XmlNodePrinter(preserveWhitespace: true) may be your friend for what you're trying to do also. See the rest of the options in the docs: http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html.