1 / 16

Utilizing XML in ColdFusion MX

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

ariadne
Download Presentation

Utilizing XML in ColdFusion MX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Utilizing XML in ColdFusion MX by Attila Domokos

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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!

  16. 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

More Related