....
I need to grab the video embed code, no...">

Groovy XmlSlurper

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

      

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

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

+2


a source to share


2 answers


This can be done 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])

      

+3


a source


def parser = new XmlSlurper().parseText(xml)
parser.'**'.findAll { it.name() == 'embedcode'}.each { embedcode ->
    //todo
    println embedcode.text()
}

      



+2


a source







All Articles