2012-05-24 1 views
0

groovy.util.slurpersupport.Nodegroovy.util.Node으로 변환하는 쉬운 방법이 있습니까?어떻게 신속하게 groovy.util.slurpersupport.Node를 인쇄합니까?

빠른 디버깅을 위해 XmlSlurper에서 오는 노드에서 XmlNodePrinter을 사용하려고합니다.

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[email protected]' with class 'groovy.util.slurpersupport.Node' to class 'groovy.util.Node' 

어떻게 신속 config의 XML 표현을 인쇄 할 수 있습니다

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] 행에 다음을 던졌습니다 : 여기 내 코드 (아마 가장 우아한)입니다?

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"?> 
+1

groovy에서이 버그와 관련이 있습니까? https : //stackoverflow.com/questions/3151095/groovy-xmlutil-serialze-is-throwing-a-content-is-not-allowed-in-prolog-error – loteq

답변

4

몇 가지 빠른 디버깅을, 가장 간단한 방법은 XmlUtil을 사용하는 방법

import groovy.xml.* 


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

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

println XmlUtil.serialize(a) 
+0

그것은 작동하지 않는 것 같습니다. 위의 편집을 참조하십시오. – GreenGiant

+0

위의 코드를 게시하기 전에 테스트했기 때문에 Groovy 콘솔에서 실행할 수있는 자체 포함 된 예제를 작성해야합니다. 이는 지옥처럼 작동합니다. – loteq

+0

이것이 저에게 효과적입니다. – Shorn