2011-04-25 2 views
6

주어진 HTML 페이지에서 HTML의 일부를 추출해야합니다.그루비로 HTML을 추출하는 부분

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) 

그러나, 나는이 얻는 결과가 보이는

<html:body xmlns:html='http://www.w3.org/1999/xhtml'>a <html:b>test</html:b></html:body> 

입니다 : 지금까지, 나는 StreamingMarkupBuilder를 사용하여 필요한 부분을 좀하려고 다음 HTML 페이지를 구문 분석하고 tagsoup으로 XmlSlurper를 사용 좋아,하지만 html - 네임 스페이스없이 그것을 얻고 싶습니다.

네임 스페이스를 피하려면 어떻게해야합니까?

답변

6

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)