开发者

Groovy XmlSlurper

开发者 https://www.devze.com 2022-12-30 04:27 出处:网络
<div id=\"videos\"> <div id=\"video\"> <embedcode>....</embedcode> </div> </div>
<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

精彩评论

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