Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML that passes isXML() but fails xmlParse() in ColdFusion

Tags:

I'm writing a test case for some ancient code that looks like this:

if (isXML(foo)) {
  try {
    bar = xmlParse(foo);
  }
  catch(any e) {
    // log error
  }
}

Blame reveals some backstory indicating we were seeing some XML strings for which isXML returned true, but for which xmlParse threw an exception of some sort.

What kind of string would produce this effect?

I've tried putting in a string I know is able to be parsed ok, then added an & in an element, but then isXML returns false. I'm not sure what else to try.

like image 856
jinglesthula Avatar asked Apr 26 '18 19:04

jinglesthula


1 Answers

Following is the usage details of IsXml() from the DOCS:

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.

So, it might be possible that some namespace has been used but the definition was not found. i.e.,

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

Output:

Is XML Valid: YES
Is XML Parsable: FALSE

Here is the GIST.

like image 160
Beginner Avatar answered Oct 02 '22 11:10

Beginner