Groovy XmlSlurper get value of the node without children

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:38:22

问题


I'm parsing HTML and trying to value of a parent node itself, without values of the children nodes.

HTML example:

<html>
    <body>
        <div>
             <a href="http://intro.com">extra stuff</a>
             Text I would like to get.
             <a href="http://example.com">link to example</a>
        </div>
    </body>
</html>

Code:

def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlSlurper(tagsoupParser)
def htmlParsed = slurper.parseText(stringToParse)

println htmlParsed.body.div[0]

However above code returns:

extra stuff Text I would like to get. link to example

How can I get only parent node value without children? Example:

Text I would like to get.

P.S: I tried removing extra elements by doing substring but it proves to be unreliable.


回答1:


If you switch to using XmlParser instead of XmlSlurper, you can do:

println htmlParsed.body.div[0].localText()[0]

Assuming you are on Groovy 2.3+



来源:https://stackoverflow.com/questions/29629006/groovy-xmlslurper-get-value-of-the-node-without-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!