160 likes | 309 Views
Utilizing XML in ColdFusion MX. by Attila Domokos. Speaker Information. Who am I? Attila Domokos Web Programmer at a Twin Cities based web development company 2+ years of ColdFusion, 5 months of CF MX experience PHP and JAVA enthusiast. Agenda. What is XML - the XML syntax
E N D
Utilizing XML in ColdFusion MX by Attila Domokos
Speaker Information Who am I? • Attila Domokos • Web Programmer at a Twin Cities based web development company • 2+ years of ColdFusion, 5 months of CF MX experience • PHP and JAVA enthusiast
Agenda • What is XML - the XML syntax • Parsing the XML document • XML element objects • Looping through child elements • XML search with XPath • Generating new XML documents • Real World Example
What is XML? • eXtensible Markup Language • XML looks a bit like HTML • XML offers a method of putting structured data into a text file • XML is machine-readable, human-intelligible • XML is license-free, platform-independent and well supported
More strict than HTML (Proper nesting and closing tags are required.) Starts with XML declarations Comments take the same form as they do in HTML (<!- - This is a comment - - >) DTD (Document Type Definition), XDR (XML Data Reduced Schema) and XSD (XML Schema Definition) are used for describing a document Validation in IE 5+ browsers XML Syntax
An Example <?xml version=“1.0” encoding=“UTF-8”?> <!- - This is my first comment - -> <company name=“Macromedia”> <location name=“San Francisco”> <employees> <person>Tom Cruise</person> <person>Ben Forta</person> <person>Nicole Kidman</person> </employees> </location> </company> Preview in Browser
Parsing the XML Document 1. Specify the file and its path first <CFSET MyXMLFile = ExpandPath(“company.xml”)> 2. Read the XML file into a string variable <CFFILE ACTION=“READ” FILE=“MyXMLFile” VARIABLE=“MyXMLCode”> 3. Parse the document with XMLParse() function into an XML “object” <CFSET MyXML = XMLParse(MyXMLCode)> VIEW
XML Element Objects I. One root element in XML documents <CFSET xnCompany = MyXml.XmlRoot> • XmlName - the name of the element <CFSET enCompany = nCompany.XmlName> • XmlAttributes – contains all the attributes of the corresponding element <CFSET sCompanyName = xnCompany.XmlAttributes[“name”]> VIEW
XML Element Objects II. • XmlChildren – An array that contains all the immediate children of the element <CFSET Locations = xnCompany.XmlChildren> The number of child level elements can be determined with the length of the array <CFSET nNumLocations = ArrayLen(Locations)> Each item contained by this array will be another XML element object (has all three properties) VIEW
Looping Through Child Elements To display all the locations in the example, you have to loop through the Locations array. <CFLOOP FROM=1 TO=“#nNumLocations#” INDEX=“I”> <CFSET xnThisLocation = Locations[i]> <CFSET sLocationName = xnThisLocation.XmlAttributes[“name”]> <CFOUTPUT><p>Locations #i# is: <b>#sLocationName#</b></p></CFOUTPUT> </CFLOOP> VIEW
Nested Loops Use nested loops if you want to display the employee’s name under locations from the example (company.xml). <CFSET arEmployees = xnThisLocation[“employees”].XmlChildren> <CFSET nNumEmployees = ArrayLen(arEmployees)> <CFLOOP FROM=1 TO=“#nNumEmployees#” INDEX=“j”> <CFOUTPUT>#arEmployees[j].XmlText#</CFOUTPUT> </CFLOOP> VIEW
XMLSearch() and XPath • Querying an XML file is available with the XMLSearch() function through XPath • XPath is a way to apply XSLT stylesheets, now think of it as the XML equivalent to the “WHERE” clause in SQL query • XPath uses slashes to represent the nested elements
Find Elements with XPath XmlSearch(MyXml, “/company/location/employees/person”) • This code will find ALL the <person> elements VIEW XmlSearch(MyXml, “/company/location[@name=‘Newton’]/employees/person”) • You’ll get only the employees belonging to the “Newton” location VIEW XmlSearch(MyXml, “/company/location[@name=‘#URL.LocName#’]/employees/person”) • Location name passed through URL VIEW
Generating New XML Docs • <CFXML> tag to output XML <CFXML VARIABLE=“MyXml”> XML code </CFXML> • Output the XML as a string – use the ToString() function • Once you have the XML code as string, you can save it to disk with <CFFILE>, upload it with <CFFTP> or send it with <CFMAIL> VIEW
Real World Example Get the latest articles from Macromedia Designer & Developer Center’s XML feed to your site! • The XML file can be found at: http://www.macromedia.com/desdev/resources/ macromedia_resources.xml • To read the file, use the <CFHTTP> tag Let’s create this .cfm template together!
Credits • Devan Shepherd - Teach Yourself XML in 21 days - (2001) - SAMS • David Hunter - Beginning XML (2000) - WROX • Utilizing XML and XSLT in ColdFusion MX - Nate Weiss (2002 April) -macromedia.com