Print groovy nodechild values

I want to print my xml, which comes from an external feed on the console. When i do

log.debug "${xml}"

I get xml values ​​in the console, but not start and end tags. for instance

 <fruits>
     <fruit1>apple</fruit1>
     <fruit2>orange</fruit2>
 </fruits>

Just prints appleorange Just the values ​​are combined one by one. What is the best value for processing it. I tried this Best way to prettyly print the XML response in grails , but I get an exception in parseText (). I do not know why, because I think the incoming xml is valid.

Update: The xml variable type is Groovy NodeChild.

+3
source share
2 answers

You can do the following, if your xml is simple, it should satisfy your needs:

`

def xml = new XmlSlurper().parseText(xmlString)
def result = new StreamingMarkupBuilder().bind{
            mkp.yield xml
            }
log.debug result as String

`

+5
source

try it

def writer = new StringWriter()
xml.writeTo(writer)
log.debug writer.toString()
0
source

Source: https://habr.com/ru/post/1755621/


All Articles