An IsXML() Function

A reader this morning asked me if there was a way to check if a block of text was XML. My initial reaction was to say “just use the IsXML() function”, but that I checked and discovered that CFML does not have an IsXML() function. It has an IsXMLDoc() function, but that checks to see if a passed value is an XML DOM, it will not check a string of text to determine if it contains XML. I’d consider this an oversight, CFML needs an IsXML() function, but for now I whipped up a really simple UDF for the reader. In case any of you could use the code, here it is. (This is a quick and dirty solution that took all of 60 seconds to code and test, if you have a more elegant one please share):

3 responses to “An IsXML() Function”

  1. Massimo Foti Avatar
    Massimo Foti

    Just a small note; I would avoid using the term "XML DOM" when referring to XML and ColdFusion. The documentation refer to "XML Doc" since the very peculiar way ColdFusion handles XML has nothing to do with W3C DOM specifications. I know this confused a few fellow developers in the past.
    Don’t get me wrong, the way ColdFusion MX handles XML is very "fusionesque", being both simpler and powerful, but the DOM is quite a different beast

  2. Sam Avatar

    I think you would have to be careful with the use-case on this function. It works fine but it seems to me that in most cases where you’re going to call isXml(), the next thing you’re going to want to do is actually parse it. If that’s the case, then you’re parsing it twice. Better to just try to parse it in the first place and catch the error, the way your isXml() does.
    nothing wrong with the function–it’s great. Just have to be careful where to use it. My $0.02.

  3. Adrian J. Moreno Avatar
    Adrian J. Moreno

    Well this solves the problem of "when taking data from a form to a db that will later be output to XML, how can we validate the data is formatted correctly before INSERT or UPDATE?" Thanks.
    <cfscript>ed version:
    <cffunction name="isXML" returntype="boolean" output="no">
    <cfargument name="data" type="string" required="yes">
    } catch(Any e) {
    return false;
    return true;

Leave a Reply