2
votes
<div id="videos">
    <div id="video">
        <embedcode>....</embedcode>   
    </div>
</div>

I need to grab the video embed code, and not just the text inside a XMl tag. Any idea how can I grab the snippet of XML using XmlSlurper?

I need the whole line: <embedcode>....</embedcode>

2

2 Answers

3
votes

This can be done by using XMLParser instead of XMLSlurper, and XMLNodePrinter:

def xml = """
<div id="videos">
    <div id="video">
        <embedcode><i>codeA</i>CodeB</embedcode>   
    </div>
</div>"""

def parser = new XmlParser().parseText(xml)
new XmlNodePrinter().print(parser.div.embedcode[0])
1
votes
def parser = new XmlSlurper().parseText(xml)
parser.'**'.findAll { it.name() == 'embedcode'}.each { embedcode ->
    //todo
    println embedcode.text()
}