我正在从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>'
关于grails - 如何避免groovy/XMLSlurper从节点剥离html标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710164/