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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With