【发布时间】:2012-05-24 20:08:09
【问题描述】:
有没有简单的方法将groovy.util.slurpersupport.Node 转换为groovy.util.Node?
我正在尝试在来自XmlSlurper 的节点上使用XmlNodePrinter,以进行快速调试。这是我的代码(可能不是最优雅的):
def xml = new XmlSlurper().parse( new File( path + pomFile ) )
def services = xml.build.plugins.plugin.configuration.services
services.children().findAll{ it.artifactId.text() == serviceName }.each { config ->
// begin section to dump "config" for debugging
def stringWriter = new StringWriter()
new XmlNodePrinter(new PrintWriter(stringWriter)).print(config[0])
println stringWriter.toString()
// end section to dump "config" for debugging
// do some other processing on the config node
}
这会在config[0] 行中引发以下内容:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'groovy.util.slurpersupport.Node@14712c3' with class 'groovy.util.slurpersupport.Node' to class 'groovy.util.Node'
如何快速打印出config 的xml 表示形式?
我仅限于 Groovy 1.7.0。
-
编辑:我也尝试了以下但收到错误:
services.children().findAll{ it.artifactId.text() == serviceName }.each { config ->
println XmlUtil.serialize(config)
这是打印的内容:
[Fatal Error] :1:1: Content is not allowed in prolog.
ERROR: 'Content is not allowed in prolog.'
<?xml version="1.0" encoding="UTF-8"?>
【问题讨论】:
-
会不会和groovy中的这个bug有关?stackoverflow.com/questions/3151095/…
标签: xml groovy markupbuilder