Coldfusion Tutorials



Spry Tutorials

Air Tutorials


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:

<?xml version="1.0" encoding="UTF-8"?>
<people>
   <person>
      <firstname>john</firstname>
      <lastname>Doe</lastname>
   </person>
</people>
To create this in Coldfusion we have two possible ways we could start with the xmlNew() function to create an empty xml Document object and then add our elements.
<cfset xmldoc = xmlNew()>
<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'>
But thats a pretty darn painful way to create an xml document. The other way is to simply convert the xml string into an xml object.
<cfset xmlText = "<people><person><firstName>John</firstName><lastName>Doe</lastName></person></people>">
<cfset xmlDoc = xmlParse(xmlText)>
So why have we created an xml document object anyway? Well in order for you to be able to manipulate xml "easily" within Coldfusion it needs to be in an internal Coldfusion XML object. Once you have it parset into an object you can do all kinds of stuff with and to it.

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 xmlDoc = xmlParse('http://www.garyrgilbert.com/tutorials/spry/assets/xml/qotd.xml')>
<cfset res = xmlSearch(xmlDoc,"/quotes/quote[author='Zach Braff']")>
<Cfoutput>#res[1].author.xmlText#:#res[1].text.xmlText#</Cfoutput>
So what exactly did I do here? I used the xmlParse function to go and grab the xml document located at the specified URL, once parsed into a Coldfusion xml object I can use the xmlSearch function with my Xpath search string to return an array of all quotes that match my search string, in this case it was only one. The xmlSearch function returns an array of xml object nodes.

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.