Working with XML
XML has become an integral part of our daily computing lives, not one day goes by that some XML somewhere is not being created and parsed. Every time you read your news via an RSS feed you are looking at transformed XML. Most web-services run on flavors of XML; REST and SOAP for example are two such. You should definitely not be afraid of XML!
Coldfusion XML Object
I'm going to assume you know what XML is and also know how it's formatted so let's get started and create
a XML document object in Coldfusion.
Lets take the following simple XML Document:
<people>
<person>
<firstname>john</firstname>
<lastname>Doe</lastname>
</person>
</people>
<cfset xmldoc.xmlRoot = xmlElemNew(xmldoc,"people")>
<cfset xmldoc.people.xmlChildren[1] = xmlElemNew(xmldoc,"person")>
<cfset xmldoc.people[1].person.xmlChildren[1] = xmlElemNew(xmldoc,"firstName")>
<cfset xmldoc.people[1].person.xmlChildren[2] = xmlElemNew(xmldoc,"lastName")>
<cfset xmldoc.people[1].person.firstname.xmlText = 'John'>
<cfset xmldoc.people[1].person.lastname.xmlText = 'Doe'>
<cfset xmlDoc = xmlParse(xmlText)>
Searching and the XML Object
So now we have an XML object we can really begin to work with it, meaning we can use xpath in conjunction with the xmlSearc() function to search our xml Document, we wouldn't be able to use xpath if it was still just a xml string. On my server I have an spry tutorial that uses an xml document of quotes of the day. If I only wanted to get the quote from Zack Braff how would I do that?
<cfset res = xmlSearch(xmlDoc,"/quotes/quote[author='Zach Braff']")>
<Cfoutput>#res[1].author.xmlText#:#res[1].text.xmlText#</Cfoutput>
Conclusion
Coldfusion provides us with a number of functions to allow us to work with xml. Using the xmlParse function allows us to work with xml documents as either and xml document object or as a standard coldfusion structure as you saw in my Zack Braff code example I accessed the xmltext using standard structure dot notation. Some other languages provide a much more exaustive array of xml functions but you should be able to get pretty far with just the built in Coldfusion functions.