加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

传递isXML()的XML但在ColdFusion中失败了xmlParse()

发布时间:2020-12-16 23:10:03 所属栏目:百科 来源:网络整理
导读:我正在为一些看起来像这样的古老代码编写测试用例: if (isXML(foo)) { try { bar = xmlParse(foo); } catch(any e) { // log error }} Blame揭示了一些背景故事,表明我们看到一些XML字符串,其中isXML返回true,但是xmlParse引发了某种异常. 什么样的字符串会
我正在为一些看起来像这样的古老代码编写测试用例:

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.

因此,有可能使用了某些命名空间但未找到定义.即,

<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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读