【发布时间】:2018-09-17 16:31:15
【问题描述】:
我试图在不使用 GPath(或)节点名称的情况下从 XML 中提取 CDATA 内容。简而言之,我想从 XML 中查找并检索包含 CDATA 部分的 innerText。
我的 XML 看起来像:
def xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Test1>This node contains some innerText. Ignore This.</Test1>
<Test2><![CDATA[this is the CDATA section i want to retrieve]]></Test2>
</root>'''
从上面的 XML 中,我想单独获取 CDATA 内容,而不使用其节点名称“Test2”的引用。因为在我的场景中节点名称并不总是相同的。
另请注意,XML 可以在其他几个节点(Test1)中包含 innerText。我不想找回那个。我只需要整个 XML 中的 CDATA 内容。
我想要类似下面的东西(虽然下面的代码不正确)
def parsedXML = new xmlSlurper().parseText(xml)
def cdataContent = parsedXML.depthFirst().findAll { it.text().startsWith('<![CDATA')}
我的输出应该是:
this is the CDATA section i want to retrieve
【问题讨论】:
-
使用 groovy xml 解析器无法检测到 cdata。您必须使用 DOM 或其他 xml 解析器。
标签: groovy cdata xmlslurper