我的问题是关于解析XML,其中字符串值中包含HTML标记:
def xmlString = '''
<resource>
<string name="my_test">No problem here!</string>
<string name="my_text">
<b> <big>My bold and big title</big></b>
Rest of the text
</string>
</resource>
'''
(这是一个Android资源文件)
当我使用XmlSlurper时,HTML标记被删除。这段代码:
def resources = new XmlSlurper().parseText(xmlString )
resources.string.each { string ->
println "string name = " + string.@name + ", string value = " + string.text()
}
将产生
string name = my_test, string value = No problem here!
string name = my_text, string value = My bold and big title
Rest of the text
我可以使用CDATA来防止HTML标记被解析,但是当使用字符串my_text时,将不会处理这些HTML标记。
我还尝试使用StreamingMarkupBuilder,如该SO答案所述:如何使用groovy从XML文件中提取HTML代码,但随后仅显示HTML标签和它们之间的文本:
<b><big>My bold and big title</big></b>
并且不显示第一个字符串。提前致谢!
def xmlString = '''
<resource>
<string name="my_test">No problem here!</string>
<string name="my_text">
<b><big>My bold and big title</big></b>
Rest of the text
</string>
</resource>
'''
def result = []
def resources = new XmlSlurper().parseText(xmlString).string
resources.each { resource ->
result << new groovy.xml.StreamingMarkupBuilder().bind { mkp.yield resource.getBody() }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句