我正在从POST响应中读取HTML文件,并使用XMLSlurper对其进行解析。页面上的textarea节点中放入了一些HTML代码(未使用Urlencode编码-不是我的选择),当我读取该值时,Groovy会剥离所有标签。

例:

<html> 
    <body> 
        <textarea><html><body>This has html code for some reason</body></html></textarea> 
    </body> 
</html> 

当我解析以上内容,然后找到(...)“textarea”节点时,它返回给我:
This has html code for some reason 

并且没有任何标签。如何保存标签?

请您参考如下方法:

我认为您正在获取正确的数据,但打印出的数据有误...您可以尝试使用StreamingMarkupBuilder将节点转换回xml吗?

def xml = '''<html> 
            |  <body> 
            |    <textarea><html><body>This has html code for some reason</body></html></textarea> 
            |  </body> 
            |</html>''' 
 
def ta = new XmlSlurper().parseText( xml ).body.textarea 
 
String content = new groovy.xml.StreamingMarkupBuilder().bind { 
  mkp.yield ta.children() 
} 
 
assert content == '<html><body>This has html code for some reason</body></html>' 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!