| By Andrew Schwabe | Article Rating: |
|
| August 1, 2007 09:30 PM EDT | Reads: |
17,303 |
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.
Published August 1, 2007 Reads 17,303
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe Fiddles with its Web Apps
- Adobe & Salesforce Cut Cloud Deal
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF

























