Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

ColdFusion Feature — Coding with XML

Reading and creating well-formed XML

If you have XML that is 10 lines long, chances are you can just take a quick look at it and figure out the problem, but if your XML Document has 2,000 rows of data, it's no longer a trivial process. That's where XmlValidate() comes into play.

This valuable and under-used function checks whether an XML Document is well formed and valid, and it also returns a list of what's wrong with the XML Document. That list is exactly what we need to "fix up" or verify that the data we have is usable.

XmlValidate() takes two arguments, the first is the XML you want to check. This can be text you read from an XML file, an XML ColdFusion Object you created, or the path and filename of the XML file on the Web server. The second argument is optional and is for the DTD, and can also be a string, URL, or path and filename.

When you call XmlValidate, it'll check your XML Document and return a struct with several valuable parts: Errors is an array that contains any errors in the document that prevent it from being valid (i.e., complying with the DTD); FatalErrors is an array that contains any errors in the document that prevent it from being well formed (and parsable using XmlParse); Status tells us whether all the tests were passed or not and returns a YES or NO; Warning is an array of any warnings that XmlValidate() found while checking the XML Document.

The FatalErrors array we get from XmlValidate is incredibly valuable. It will tell you which line and at which character it found a problem, and in my experience provides relatively helpful error messages.

Searching XML Documents Using XmlSearch()
We now understand that we can parse an XML Document and get a ColdFusion XML Object that is a bunch of arrays and structures, so we can read pieces as we want.

What if you only want to pull out a few parts of an XML Document, or if you are only interested in one type of XML Element from your document?

XmlSearch is designed to let you search your XML document for specifically named XML Elements. Let's look again at Listing 2. If we wanted to get an array of all the <item> elements, we could refer to the XmlChildren array as we did before. But what if this XML Document contained multiple orders? Then for each order there would be a different set of <item> elements, one set for each order. Getting a list of all the <item> elements is a whole lot more complex in this scenario. Instead of trying to pull apart the information we want, let's use XmlSearch() instead (see Listing 6).

XmlSearch() takes two parameters, the first is your ColdFusion XML Object and the second is an XPath expression. XPath, according to Wikipedia, is an expression language for addressing portions of an XML Document. We are only going to use it for basic purposes, but if you want to read up on XPath, a good place to start is with www.w3.org/TR/xpath.

On line 8 we are calling XmlSearch. The first argument is the XML Object we are working with, and the second is the XPath expression. This simple expression simply states "All XML Elements where the XMLName of the Element is Item."

The return type from XmlSearch is an array, so we save the results in an array variable called myResultsArray.

In the results, each item of the array is the actual XML Element that XmlSearch() found. This means that each search result will have the same Structure values as any other XML element, including XMLName, XMLText, XMLAttributes, and XMLChildren.

Once you have your results, you can use any of the techniques above to read and use the data that was found.

What to Study Next
We certainly can't cover every aspect of programming with XML here, but luckily you have the Internet at your fingertips.

Some of the recommended topics for study are listed below:

XPath Language - http://en.wikipedia.org/wiki/XPath
CDATA (character data embedded in XML) - http://en.wikipedia.org/wiki/CDATA
XSL - http://en.wikipedia.org/wiki/Extensible_Stylesheet_Language
XmlTransform() - ColdFusion function - search Google

Summary
I hope this has been a useful overview of coding with XML and the various built-in tools in ColdFusion. These tools should help get started with reading and creating well-formed XML.

More Stories By Andrew Schwabe

Andrew C. Schwabe is president of IEXP Software, LLC and is a veteran CFML developer since 1998. Prior to working with IEXP, Andrew worked for 10 years as the president and chairman of the Board of Internet Expressions, Inc. He has been the forerunner in developing numerous applications ranging from the free - CFX_RawSocket (a Java Custom Tag for ColdFusion) - to FusionDox (enterprise-level document management). Andrew has been deeply involved in developing many of these applications from the ground up, enhancing and using the ColdFusion platform to its fullest potential. In addition to his extensive ColdFusion experience, he has been recognized in the business community, receiving the coveted Dale Carnegie Highest Achievement Award.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.