开发者

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()
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜