没有XML声明的GPathResult到String

Mic*_*das 5 xml groovy gpath

我正在GPathResult转向String使用

def gPathResult = new XmlSlurper().parseText('<node/>')
XmlUtil.serialize(gPathResult)
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我在XML前面得到XML声明

<?xml version="1.0" encoding="UTF-8"?><node/>
Run Code Online (Sandbox Code Playgroud)

如何在一开始GPathResultString没有转换<?xml version="1.0" encoding="UTF-8"?>

the*_*th_ 9

使用XmlParser而不是XmlSlurper:

def root = new XmlParser().parseText('<node/>')
new XmlNodePrinter().print(root)
Run Code Online (Sandbox Code Playgroud)

使用new XmlNodePrinter(preserveWhitespace: true)可能是你的朋友,你也想做什么.请参阅文档中的其他选项:http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html.