我正在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)
如何在一开始GPathResult就String没有转换<?xml version="1.0" encoding="UTF-8"?>?
使用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.