xml processing in coldfusion mx everything you wanted to know about xml, but were afraid to ask...

26
XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Upload: jeffry-singleton

Post on 28-Dec-2015

248 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XML processing in ColdFusion MXEverything you wanted to know about XML, but were afraid to askOctober 2006 Jaxfusion User Group

Page 2: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

About the Presenter

» David Fekke» Working with XML 1999» Lead for Vurv HR-XML API

Page 3: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Covered in this presentation

» Properly formed XML» ColdFusion XML Tags and functions» XPath queries» XML XSDs and Validation» XML Namespaces» DOM vs. Streaming parsers» StAX API

Page 4: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XML

» eXtensible Markup Language» Originated from SGML» W3C standard» More versatile then Flat files

Page 5: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XML Syntax

» XML declaration» <?xml version="1.0" encoding="UTF-8"?>» Must have one root tag» <pre:roottag> </pre:roottag>» All tags must have closing tag.» <br />: single tags can have forward slash

at the end» <tag name=“value” /> tags can have

attributes

Page 6: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Pre MX XML features

» WDDX» SOXML custom tag, windows only

Page 7: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

ColdFusion MX 6

» CFXML tag turns serialized XML into a ColdFusion object

» XMLParse() function» ColdFusion XML Object works like series of

structures and arrays» XMLSearch() function» XMLSearch Uses XPath queries to return

array of XML nodes

Page 10: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

ColdFusion MX 7 Functions

» XmlGetNodeType() » XmlValidate()

Page 11: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XPath queries

» Provides a way to pull the elements you need without having to iterate through the entire DOM

» /rootelement/parent/childelement» //childelement» /rootelement/parent[childelement = “foo”]» Used with XSLT templates

Page 12: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XML Validation

» Define XML with DTD or XSD» XSD preferred method for validation» XSD uses an XML format» Allows for strict and custom datatypes » Allows for pattern recognition

Page 13: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="FirstName" type="xs:string"/><xs:element name="LastName" type="xs:string"/><xs:element name="Age" type="xs:string"/><xs:element name="customer">

<xs:complexType><xs:sequence>

<xs:element ref="FirstName" /><xs:element ref="LastName" /><xs:element ref="Age" />

</xs:sequence></xs:complexType>

</xs:element><xs:element name="customers">

<xs:complexType><xs:sequence>

<xs:element ref="customer" maxOccurs="unbounded" />

</xs:sequence></xs:complexType>

</xs:element></xs:schema>

Page 14: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

<?xml version="1.0" encoding="UTF-8" ?><customers> <customer> <FirstName>Dwight</FirstName> <LastName>Mercer</LastName> <Age>32</Age> </customer> <customer> <FirstName>Miguel</FirstName> <LastName>Cabrera</LastName> <Age>33</Age> </customer></customers>

Page 15: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Default XSD element types

» xs:String» xs:normalizedString » xs:integer» xs:base64Binary » xs:negativeInteger» xs:positiveInteger» xs:Hexbinary» xs:Long» xs:short

Page 16: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Custom Types

<xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string">

<xsd:pattern value="\d{3}-[A-Z]{2}"/></xsd:restriction>

</xsd:simpleType>

Page 17: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XML Namespaces

» Namespaces allow for XML to be segmented into different parts

» <namespace:elementName /> namespace will prefix the element name

» <fekke:root xmlns:fekke=“http://www.fekke...

» No namespace xmlns=“”» Use explicit XPath with no namespace» Nonamespace xpath=“/:element”

Page 18: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

DOM, SAX and Pull Parsers

» ColdFusion uses a DOM parser. Entire XML object in memory.

» SAX parser reads in file portion of the time. Raises events when elements match.

» StAX uses pull based parser to read one node at time. Uses streams to read in portion

Page 19: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

StAX

» BEA JSR 173» Many implementations» Includes Event and Pull parsers» Jim Collins working on CFStAX library» Woodstox library extremely fast

implementation

Page 20: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

StAX objects

» XMLInputFactory» XMLStreamConstants» XMLStreamReader» XMLEventReader

Page 21: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XMLStreamConstants

» Constants for different node types» XMLStreamConstants.START_ELEMENT» XMLStreamConstants.END_ELEMENT» XMLStreamConstants.START_DOCUMENT» XMLStreamConstants.END_DOCUMENT

Page 22: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

XmlStreamReader methods

» Next() iterates through XML stream» getLocalName() returns tag name» getAttributeCount() returns tags attribute

count» getAttributeLocalName() returns attribute

name» getAttributeValue() returns attribute value» hasText() returns boolean if element has text» getText() returns element text

Page 23: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Free XML Tools

» IE and Firefox have built in DOM parsers» XMLFox for testing validation» XMLButterfly for editing and formatting

XML» Don’t use ALtova XML Spy, its crap!

Page 24: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Summary

» Use XMLSearch instead of iterating through DOM

» Don’t use cfsavecontent to build XML, use CFXML

» If using no namespace, use explicit xpaths» Use xmlFormat or CDATA to handle special

characters» Use XSDs over DTDs

Page 25: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Links

» http://www.w3.org/XML/» http://www.w3.org/XML/Schema» http://www.xml.com/» http://coldfusion.sys-con.com/read/

236002.htm

Page 26: XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group

Books

» ColdFusion Application Development» Beginning XML (Programmer to

Programmer)» O’reilly XML in a Nutshell