【问题标题】:How do I quickly print a groovy.util.slurpersupport.Node?如何快速打印 groovy.util.slurpersupport.Node?
【发布时间】: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"?>

【问题讨论】:

标签: xml groovy markupbuilder


【解决方案1】:

对于一些快速调试,最简单的方法是使用 XmlUtil

import groovy.xml.*


def xml="""
<a><b>b</b><c/></a>
"""

def a=new XmlSlurper().parseText(xml)

println XmlUtil.serialize(a)

【讨论】:

  • 你必须提供一个独立的例子,我们可以在一个 groovy 控制台中运行,因为我在发布之前测试了上面的代码,它确实有效
猜你喜欢
  • 2022-11-18
  • 2017-06-09
  • 2013-05-12
  • 1970-01-01
  • 2012-09-30
  • 2015-01-09
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
相关资源
最近更新 更多