XML Parsing in Groovy strips attribute new lines

Posted by Bill James on Stack Overflow See other posts from Stack Overflow or by Bill James
Published on 2010-04-30T18:45:41Z Indexed on 2010/04/30 18:47 UTC
Read the original article Hit count: 238

Filed under:
|
|

I'm writing code where I retrieve XML from a web api, then parse that XML using Groovy. Unfortunately, it seems that both XmlParser and XmlSlurper for Groovy strip newline characters from the attributes of nodes when .text() is called.

How can I get at the text of the attribute including the newlines?

Sample code:

def xmltest = '''
<snippet>
   <preSnippet att1="testatt1" code="This is line 1
   This is line 2
   This is line 3" >
      <lines count="10" />
   </preSnippet>
</snippet>'''

def parsed = new XmlParser().parseText( xmltest )
println "Parsed"
parsed.preSnippet.each { pre ->
       println pre.attribute('code');
}


def slurped = new XmlSlurper().parseText( xmltest )
println "Slurped"
slurped.children().each { preSnip ->
   println [email protected]()
   }

the output of which is:

Parsed
This is line 1    This is line 2    This is line 3
Slurped
This is line 1    This is line 2    This is line 3

© Stack Overflow or respective owner

Related posts about groovy

Related posts about Xml