用groovy提取HTML部分

rdm*_*ler 6 html groovy xmlslurper

我需要从给定的HTML页面中提取HTML的一部分.到目前为止,我使用带有tagsoup的XmlSlurper来解析HTML页面,然后尝试使用StreamingMarkupBuilder获取所需的部分:

import groovy.xml.StreamingMarkupBuilder
def html = "<html><body>a <b>test</b></body></html>"
def dom = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser()).parseText(html)
println    new StreamingMarkupBuilder().bindNode(dom.body)
Run Code Online (Sandbox Code Playgroud)

但是,我得到的结果是

<html:body xmlns:html='http://www.w3.org/1999/xhtml'>a <html:b>test</html:b></html:body>
Run Code Online (Sandbox Code Playgroud)

看起来很棒,但我想在没有html命名空间的情况下得到它.

如何避免命名空间?

ata*_*lor 7

关闭TagSoup解析器上的命名空间功能.例:

import groovy.xml.StreamingMarkupBuilder
def html = "<html><body>a <b>test</b></body></html>"
def parser = new org.ccil.cowan.tagsoup.Parser()
parser.setFeature(parser.namespacesFeature, false)
def dom = new XmlSlurper(parser).parseText(html)
println new StreamingMarkupBuilder().bindNode(dom.body)
Run Code Online (Sandbox Code Playgroud)