如何在Groovy中解析XML注释? [英] How to parse XML comments in Groovy?
本文介绍了如何在Groovy中解析XML注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Groovy中可以解析XML注释吗?
Is there any way to parse XML comments in Groovy?
XMLParser和XMLSluprer似乎都不支持注释节点.
Both XMLParser and XMLSluprer don't seem to support comments nodes.
假设以下文件(example.html):
Suppose following file (example.html):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title</title>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">title</td></tr>
</thead><tbody>
<!--I cannot be seen-->
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
</tbody></table>
</body>
</html>
这是我的代码:
def parser = new XmlSlurper(false, false)
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
def response = parser.parse('example.html')
当我使用
println XmlUtil.serialize(response)
要输出文件,看不到任何注释.
to output the file, no comment can be seen.
推荐答案
一旦有了html,就可以使用jsoup进行解析
as soon as you have html - it's possible to use jsoup to parse
@Grab(group='org.jsoup', module='jsoup', version='1.11.3')
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
def html = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title</title>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">title</td></tr>
</thead><tbody>
<!--I cannot be seen-->
<tr>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
</tbody></table>
</body>
</html>'''
Document doc = Jsoup.parse(html)
println doc.select('html body table tbody').first()?.childNodes()?.find{it.nodeName()=='#comment'}?.getData()
这篇关于如何在Groovy中解析XML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文