0
def page = new XmlSlurper(new SAXParser()).parse(url)
println  page.body[0]

I want output

 <body>
   <h1>Header</h1>
 </body>

where my html is:

   <html>
       <head>
           <title>Title</title>
       </head>
       <body>
             <h1>Header</h1>
       </body>
   </html>

But my output is

Header

How to tell xmlSluper to take the code, not the content?

Xelian
  • 16,680
  • 25
  • 99
  • 152
  • 2
    Not at a computer, so this is a guess `println XmlUtil.serialise( page.body[0])` – tim_yates Mar 28 '14 at 13:55
  • 1
    Correct `XmlUtil.serialize( page.body[0] )`. `.serialize()` with a `z`..... [*We all live in Americaaa*](http://www.youtube.com/watch?v=xFVdvXGIT34).... :P @tim_yates – dmahapatro Mar 28 '14 at 14:07
  • 1
    Stupid phone autocorrect ;-) – tim_yates Mar 28 '14 at 14:35
  • OK. That works, but can we use some of XmlSluper methods, instead of this *util* method. Just ask. Yours answers are good for me. – Xelian Mar 28 '14 at 14:49
  • @Xelian Unfortunatley not, `XmlSlurper` doesn't handle serialization, you need to use `XmlUtil` methods or something like `StreamingMarkupBuilder` – tim_yates Mar 31 '14 at 12:04

1 Answers1

1

To serialize data, you need to use some sort of serializer such as XmlUtil.serialize or StreamingMarkupBuilder, ie:

println XmlUtil.serialize( page.body[0] )

or:

new groovy.xml.StreamingMarkupBuilder().bind { mkp.yield page.body }.toString()
tim_yates
  • 167,322
  • 27
  • 342
  • 338