Presentation on theme: "Leveraging XML with ColdFusion Jeff Tapper June 2004."— Presentation transcript:
Leveraging XML with ColdFusion Jeff Tapper June 2004
Who am I? Jeff Tapper ColdFusion Developer since 1995 Authored / Edited 6 books on ColdFusion and/or Flash Technologies Macromedia Certified Professional Advanced ColdFusion MX Developer Flash MX Developer Certified Instructor for all CF/Flash Development courses Worked on hundreds of ColdFusion Projects, clients including Toys R Us, Dow Jones, American Express, Allaire, etc. Chief Technologist – Tapper.net Consulting
Agenda What is XML How is XML useful ColdFusion and XML XML Document vs. XML Object Parsing XML Creating XML Working with XML in ColdFusion Accessing Elements Searching XML Transforming XML Summary and Questions
What is XML eXtensible Markup Language Platform independent description of data W3C Standard Tag Based Easily describes hierarchies within data Useful solution to many problems What XML isnt XML is not HTML Not a presentation language Not a programming language
How is XML Useful Data Sharing Inter-company Data Transfer Standardized method to describe data being transferred. Replaces CSV, EDI, etc. Configuration Files Ability to write a single XML file with configuration data usable by CF, Java, Flash, etc. Multilingual Content Store various language versions of same content in single file.
XML and ColdFusion XML Documents vs. XML Objects XML Documents Text files Tag based Can be based on Schema or DTD (not covered here) Can be parsed to XML object XML Objects Native CF Datatype Usable as CF Variable, like a structure or array Can be written to text file to create XML Document Can be search and transformed.
XML Document OOP with ActionScript 2 Paperback 11/2003 Tapper, Jeff Haffner, Robin Talbot, James Towes, Kevin 540 New Riders
XML Terminology OOP with ActionScript 2 Paperback 11/2003 Tapper, Jeff Haffner, Robin Talbot, James Towes, Kevin 540 New Riders Root Node
XML Terminology OOP with ActionScript 2 Paperback 11/2003 Tapper, Jeff Haffner, Robin Talbot, James Towes, Kevin 540 New Riders Root Node Tag Attribute Attribute Value
XML Terminology OOP with ActionScript 2 Paperback 11/2003 Tapper, Jeff Haffner, Robin Talbot, James Towes, Kevin 540 New Riders Root Node Tag Attribute Attribute Value Child Tags Tag Contents
XML and ColdFusion XML Object Native Datatype within ColdFusion Can be parsed from XML Document Read XML File with CFFILE, CFFTP or CFHTTP XMLParse() Can be built from scratch from CF CFXML XMLNew() XMLElemNew()
XML and ColdFusion Parsing XML Object from document
XML and ColdFusion Creating XML Object with CFXML Object Oriented Programming with ActionScript 2...
XML and ColdFusion Creating XML Object with CFXML from a query #title# #LastName#, #firstName#... #DateFormat(publicationDate,"mm/yyyy")# #pages# #style# #publisher#
XML and ColdFusion Creating/Modifying XML Object with XML Functions
Working with XML Objects Can be parsed as a native data type Can be searched with xmlSearch Uses xPath to query xml Returns an array Can be transformed using xmlTransform Convert to other formats Uses XSL, XSLT
Working with XML Objects Using XML Object natively Structure and Array syntax can be used Outputs “Object Oriented Programming with ActionScript 2 #xBooks.books.book.title.xmlText#
Working with XML Objects Using Array and Structure functions #arrayLen(xBooks.books.book)#5 #arrayLen(xBooks.books.book.authors.author) # 3 #arrayLen(xBooks.books.book.authors.contrib utionAuthors.author)# 9 #structKeyList(xBooks.books.book)#title, authors, pages, style, publicationDate, publisher #StructCount(xBooks.books.book)#6
xmlSearch Two attributes Name of XML object to search XPath expression with which to search "SQL" for XML objects Full language with syntax specified by W3C Returns an array of matching elements
XMLSearch() Example Returns an array of the 2 books with 'Forta, Ben' as an author
XPath Rules Single slashes represent nested elements in a document structure—just like slashes in a URL represent nested folders Double slashes are a wildcard meaning in any parent element – any number of nesting levels XPath comparison statements are enclosed in square brackets Non-numeric attribute or content values to match are enclosed in single quotation marks Attribute names are preceded by symbol XPath searches are case-sensitive
XMLSearch() Examples Function CallArra y Len Description xmlSearch(xBooks, 4']") 1All child book elements of the books element that have an ISBN attribute equal to xmlSearch(xBooks, "/books/book/title") 5All title elements that are children of any book element in the books element. xmlSearch(xBooks,"//title")5All title elements located in the xBooks object. xmlSearch(xBooks,"//book[autho rs/author='Forta, Ben']/title") 2All title elements that are children of the book element that has an author named 'Forta, Ben' xmlSearch(xBooks,"//book [publisher='New Riders']") 2All book elements with child element publisher contents equal to ‘ New Riders ’.
Transforming XML with xmlTransform() Uses XSLT to apply an XSL Style Sheet to an XML Document Takes two arguments: XML and XSL First argument can be either XML Object or Document Second argument must be text of XSL document
xmlTransform() Example #variables.htmlContent#
xmlTransform() – XSL File W3C published XSL specification File must be available as a variable in CF (CFHTTP, CFFTP, CFPOP, CFFILE, etc.) Capable of transforming XML document to any text format. Frequently used for showing XML as HTML Useful to transform from one form of XML to another
Summary ColdFusion MX includes a wide range of XML Tools XML is (finally!) a native data type in CFMX Same XML files can be read by several different technologies CFMX, Flash, Java,.Net, PHP, etc. XML files are searchable Easily transformed into other types of text files