Parsing XML Documents


An XML document by itself is just a string of text. To harness the power of XML, you must parse it to create an object that ColdFusion can access much like a collection of structures and arrays.

Using XmlParse()

To parse an XML document stored somewhere on disk, use <cffile> and XmlParse(), as demonstrated in Listing 14.2.

Listing 14.2. ParseXML.cfmParsing an XML Document on Disk
 <cffile action="READ"     file="#ExpandPath('CompaniesAndEmployees.xml')#"     variable="xmlDocument"> <cfset xmlObject = XmlParse(xmlDocument, "Yes")> <cfdump var="#xmlObject#"> 

Figure 14.1 shows the output of xmlObject in the browser.

Figure 14.1. A ColdFusion XML object as displayed by <cfdump>.


That was pretty painless, wasn't it? By parsing the document contents with the XmlParse() function, we converted the XML document into an object that ColdFusion recognizes as a set of employees, rather than just as a string of text.

The "Yes" is the value of the caseSensitive argument to XmlParse(), which makes the created object case-sensitive.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net