证明 XML 在 Nifi 上的格式正确

Prove XML is Well Formed on Nifi

我想证明我的 XML 在 Nifi 上格式正确。

我已经根据某些架构验证了它,但有时我没有架构,因此只想确认它的格式是否正确。然而,所有 XML 处理器似乎都需要一个模式或类似的东西,而不仅仅是显示它的格式正确。

谁能提供一些帮助?非常感谢

只需尝试使用任何一致的 XML 解析器来解析它。

如果parses successfully,即没有致命错误, 然后是 well-formed:

well-formedness constraint

[Definition: A rule which applies to all well-formed XML documents. Violations of well-formedness constraints are fatal errors.]

添加到 kjhughes 的答案(特定于 NiFi),如果你总是知道根标签(我们称之为 "root"),你可以使用 this SO post with the ValidateXml 中提到的 XSD处理器。

如果您不知道根标签,可以使用 ExecuteGroovyScript 并将 "Failure strategy" 属性 设置为 "transfer to failure" 并将 "Script Body" 设置为以下脚本:

def flowFile = session.get()
if(!flowFile) return
InputStream i = flowFile.read()
new XmlSlurper().parse(i)
i.close()
REL_SUCCESS << flowFile