0

を に変換する簡単な方法はありgroovy.util.slurpersupport.Nodeますgroovy.util.Nodeか?

簡単なデバッグのために、XmlNodePrinterからのノードでを使用しようとしています。XmlSlurperこれが私のコードです(おそらく最もエレガントではありません):

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'

のxml表現をすばやく印刷するにはどうすればよいconfigですか?

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"?>
4

1 に答える 1

6

デバッグを簡単に行うには、XmlUtil を使用するのが最も簡単な方法です。

import groovy.xml.*


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

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

println XmlUtil.serialize(a)
于 2012-05-25T21:27:11.637 に答える