【发布时间】:2018-07-17 10:47:41
【问题描述】:
有什么方法可以在 Groovy 中解析 XML cmets?
XMLParser 和 XMLSluprer 似乎都不支持 cmets 节点。
假设以下文件(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)
输出文件,看不到注释。
【问题讨论】:
-
在使用 groovy 解析器时无法获取 cmets。但你可以使用 DOM 或其他 xml 模型来获取 cmets。
标签: xml groovy xml-parsing xmlslurper