Groovy XmlSlurper
<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>....<开发者_JAVA百科/embedcode>
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])
def parser = new XmlSlurper().parseText(xml)
parser.'**'.findAll { it.name() == 'embedcode'}.each { embedcode ->
//todo
println embedcode.text()
}
精彩评论