tags:

views:

80

answers:

3

I want to print my xml which is coming from an external feed on the console. When I do

log.debug "${xml}"

I get xml values on the console but not the starting and end tags. For example

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

Just prints appleorange Just the values concatenated one after other. What is the best value to handle it. I tried this http://stackoverflow.com/questions/231677/best-way-to-pretty-print-xml-response-in-grails but I get exception at parseText(). I don't know why, because I think the incoming xml is valid.

Update: The type of variable xml is Groovy's NodeChild.

A: 

Of which type is xml? Because if it is a string it just works.

hackbert
Yeah. I realized that I am using wrong mechanism to print the xml. My xml variable is not the plain xml string. it is Groovy's NodeChild
Paras
A: 

try this

def writer = new StringWriter()
xml.writeTo(writer)
log.debug writer.toString()
Olexandr
Hi Olexandr, this doesn't work too. Gives the same output
Paras
+2  A: 

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

`

stan229
It's note a simple xml. The original question is confusing. I will update it. The variable xml is in fact a NodeChild object
Paras
My answer would work then. XmlSlurper.parseText returns a NodeChild object
stan229
Yes, it works. thanks
Paras