Coding With XML Andrew Schwabe
June 27 th - 30 th 2007www.cfunited.com What This Presentation Is Introduction to XML using ColdFusion How and where XML is used in “real” life (at least a programmers life) How to read, use and create well- formed XML documents
June 27 th - 30 th 2007www.cfunited.com Who this presentation is for You like ColdFusion You know how to use structures and arrays You need to (or hope some day to) use XML in your applications
June 27 th - 30 th 2007www.cfunited.com About Andrew Schwabe ColdFusion developer since version 3 Worked for web development company for 9 years CTO with IEXP Software Lead Architect for FusionDox IDM Document Management Software (CF based document management)
June 27 th - 30 th 2007www.cfunited.com What is XML ?
June 27 th - 30 th 2007www.cfunited.com What is XML ? Wikipedia says: Extensible Markup Language (XML) is a general-purpose markup language. It is classified as an extensible language because it allows its users to define their own tags. Its primary purpose is to facilitate the sharing of data across different information systems, particularly via the Internet. XML is meant to be relatively human- readable.
June 27 th - 30 th 2007www.cfunited.com What is XML ? eXtensible Markup Language XML is a standard for creating markup languages that describe the structure of data In some form or another (i.e. GML etc.) it has been around since the 70’s
June 27 th - 30 th 2007www.cfunited.com What is XML ? eXtensible Markup Language XML is a standard for creating markup languages that describe the structure of data In some form or another (i.e. GML etc.) it has been around since the 70’s This isn’t a history lesson. We have Google
June 27 th - 30 th 2007www.cfunited.com In Simplest Terms An XML “document” is a tree of nested elements. Elements use tag syntax to describe information Elements can contain other elements, attributes and text
June 27 th - 30 th 2007www.cfunited.com
June 27 th - 30 th 2007www.cfunited.com Benefits of Storing Data in XML XML is designed for storing JUST data Forces you to separate data from formatting (good practice!) Human Readable (for the most part) Highly portable between different development languages and operating systems
June 27 th - 30 th 2007www.cfunited.com Related Technologies Hyper Text Markup Language (HTML) RSS Really Simple Syndication Rich Site Syndication Rich Syndication Standard Rich Site Summary
June 27 th - 30 th 2007www.cfunited.com Related (cont.) XSLT (Extensible Style sheet Language) WDDX (Web Distributed Data Exchange) Web Services – SOAP (Simple Object Access Protocol) MXML, and many more…
June 27 th - 30 th 2007www.cfunited.com Where Do We Use XML ? Web Services and RSS Electronic Data Interchange (EDI) Online XML Services (i.e.. UPS, FedEx, USPS) Storing complex structures of data in files and database fields
June 27 th - 30 th 2007www.cfunited.com Where Do We Use XML ? Exchanging data with Flash Movies (Not using Flash Remoting) Reading and Storing content for use by other systems
June 27 th - 30 th 2007www.cfunited.com A Practical Example
June 27 th - 30 th 2007www.cfunited.com A Practical Example Company X Has an E-Commerce Website Hosted by an ISP Orders are taken online and stored in the web database An outside Fulfillment firm needs to have order data to package and ship orders
June 27 th - 30 th 2007www.cfunited.com A Practical Example (cont.) The goal is to maintain security and eliminate double entry (and thus removing human error) Solution: Push orders from the website in XML format and import into fulfillment system
June 27 th - 30 th 2007www.cfunited.com
June 27 th - 30 th 2007www.cfunited.com Anatomy of an XML Document
June 27 th - 30 th 2007www.cfunited.com Anatomy of an XML Document Prolog Body Elements Attributes CDATA Special Characters
June 27 th - 30 th 2007www.cfunited.com Anatomy of an XML Document 512 Goshen Road Bordell OH } Prolog } Body
June 27 th - 30 th 2007www.cfunited.com Anatomy of an XML Document 512 Goshen Road Bordell OH Attribute Element XML Text
June 27 th - 30 th 2007www.cfunited.com Anatomy of an XML Document Arm & Hammer <![CDATA[ This text will be treated as literal, and can contain reserved unescaped characters. ]]> Escaped reserved character CDATA Block
June 27 th - 30 th 2007www.cfunited.com Important Terminology
June 27 th - 30 th 2007www.cfunited.com Important Terminology “Document Type Definition (DTD)” : A DTD defines strict rules for what can be contained in a specific type of XML Document
June 27 th - 30 th 2007www.cfunited.com Important Terminology “Well Formed”: An XML document is well formed if it adheres to all the standard rules for XML (i.e. only one root element, no invalid characters, etc.)
June 27 th - 30 th 2007www.cfunited.com Important Terminology “Valid”: An XML document is valid if it is well formed, and also if it adheres to the rules of it’s DTD
June 27 th - 30 th 2007www.cfunited.com Important Terminology NOTE: As a general rule, XML documents do not specifically have to have a DTD XML Documents can be well formed without a DTD and be usable
June 27 th - 30 th 2007www.cfunited.com SerializationSerialization The process of converting textual data into an XML object is called deserialization The process of converting an XML object back into text is called serialization Web Services handle serialization and deserialization automatically
June 27 th - 30 th 2007www.cfunited.com ColdFusion Support ColdFusion MX 6 ColdFusion MX 7 ColdFusion MX 8 BlueDragon Offers Similar and Enhanced functionality
June 27 th - 30 th 2007www.cfunited.com XML Basic Functions XmlNew XmlParse XmlElemNew XmlSearch XmlValidate
June 27 th - 30 th 2007www.cfunited.com Reading an XML Document Once an XML document is converted to an XML Object in ColdFusion, it becomes a collection of structures and arrays The process of serializing local, transfer data, deserialize remotely is the key to XML
June 27 th - 30 th 2007www.cfunited.com Traversing XML Looping over XML Structure
June 27 th - 30 th 2007www.cfunited.com Creating a New XML Doc Cheating
June 27 th - 30 th 2007www.cfunited.com Creating a New XML Doc Cheating Build Using XmlNew and Arrays and Nodes
June 27 th - 30 th 2007www.cfunited.com Storing and Retrieving XML CFFILE CFHTTP Web Services
June 27 th - 30 th 2007www.cfunited.com Using WDDX Web Distributed Data Exchange Serializing Arrays and Structures Deserializing WDDX Packets
June 27 th - 30 th 2007www.cfunited.com Using WDDX Benefits Easily convert existing CF constructs into XML Deserializing WDDX data results in a standard CF construct
June 27 th - 30 th 2007www.cfunited.com XML or WDDX ? Who will need to access the data? Think about the future WDDX is not very widely adopted
June 27 th - 30 th 2007www.cfunited.com Validating XML XMLValidate Optional: specify a DTD Returns “errors” – determine if the XML Document is “Valid” Returns “fatalerrors” – determine if the XML Document is “Well Formed”
June 27 th - 30 th 2007www.cfunited.com XML Search
June 27 th - 30 th 2007www.cfunited.com ExamplesExamples XML Site Navigation Create the definition of a navigation system in XML Read the XML and dynamically build the navigation system
June 27 th - 30 th 2007www.cfunited.com Managed Website Prefs Website settings stored in XML Web page to allow users to modify settings
June 27 th - 30 th 2007www.cfunited.com Fedex Rate Lookup Retrieve dynamic rate service quotes using UPS online tools
June 27 th - 30 th 2007www.cfunited.com Tips and Tricks Dealing with HTTP Compression and Web Services
June 27 th - 30 th 2007www.cfunited.com Where to look next Google XML Support in SQL Server 2005
June 27 th - 30 th 2007www.cfunited.com Coding With XML - Updated Stuff Copy of updated presentation and sample files are posted at: Contact me at: (courtesy of engrish.com)