我正在为一些看起来像这样的古老代码编写测试用例:
if (isXML(foo)) {
try {
bar = xmlParse(foo);
}
catch(any e) {
// log error
}
}
Blame 揭示了一些背景故事,表明我们看到了一些 XML 字符串,isXML
返回 true,但 xmlParse
抛出了某种异常。
什么样的字符串会产生这种效果?
我试过放入一个我知道可以正常解析的字符串,然后在元素中添加一个 &
,但是 isXML
返回 false。我不确定还能尝试什么。
最佳答案
以下是来自 DOCS 的 IsXml()
的使用细节:
This function determines whether text is well-formed XML, that is, it conforms to all XML syntax and structuring rules. The string does not have to be a complete XML document. The function does not validate against a Document Type Definition (DTD) or XML Schema.
因此,可能使用了某些 namespace 但未找到定义。即,
<cfsavecontent variable="xml">
<?xml version="1.0" encoding="UTF-8"?>
<xyz:note>
<xyz:to>Myself</xyz:to>
<xyz:from>You</xyz:from>
<xyz:heading>Reminder</xyz:heading>
<xyz:body>Test</xyz:body>
</xyz:note>
</cfsavecontent>
<cfset xml = trim( xml )>
<!--- Try to parse --->
<cfset isXmlParsable = TRUE>
<cftry>
<cfset XmlParse( xml )>
<cfcatch>
<!--- Will come here as xyz namespace is not defined --->
<cfset isXmlParsable = FALSE>
</cfcatch>
</cftry>
<cfoutput>
Is XML Valid: #IsXml( xml )#<br>
Is XML Parsable: #isXmlParsable#
</cfoutput>
输出:
XML 是否有效:是
XML 是否可解析:假
这是 GIST
.
关于在 ColdFusion 中通过 isXML() 但 xmlParse() 失败的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50050724/