XML processing in ColdFusion MX - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

XML processing in ColdFusion MX

Description:

XML processing in ColdFusion MX. Everything you wanted to ... LastName Mercer /LastName Age 32 /Age /customer customer FirstName Miguel /FirstName ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 27
Provided by: spencer5
Category:

less

Transcript and Presenter's Notes

Title: XML processing in ColdFusion MX


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

2
About the Presenter
  • David Fekke
  • Working with XML 1999
  • Lead for Vurv HR-XML API

3
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

4
XML
  • eXtensible Markup Language
  • Originated from SGML
  • W3C standard
  • More versatile then Flat files

5
XML Syntax
  • XML declaration
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • Must have one root tag
  • ltpreroottaggt lt/preroottaggt
  • All tags must have closing tag.
  • ltbr /gt single tags can have forward slash at the
    end
  • lttag namevalue /gt tags can have attributes

6
Pre MX XML features
  • WDDX
  • SOXML custom tag, windows only

7
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

8
XML Check functions
  • IsXML
  • IsXmlDoc
  • IsWDDX
  • IsXmlAttribute
  • IsXmlElem
  • IsXmlNode
  • IsXmlRoot

9
XML editing functions
  • XmlNew()
  • XmlChildPos()
  • XmlNew()
  • XmlTransform()
  • XmlElemNew()

10
ColdFusion MX 7 Functions
  • XmlGetNodeType()
  • XmlValidate()

11
XPath queries
  • Provides a way to pull the elements you need
    without having to iterate through the entire DOM
  • /rootelement/parent/childelement
  • //childelement
  • /rootelement/parentchildelement foo
  • Used with XSLT templates

12
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

13
ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema"gt ltxselement name"FirstName"
type"xsstring"/gt ltxselement name"LastName"
type"xsstring"/gt ltxselement name"Age"
type"xsstring"/gt ltxselement
name"customer"gt ltxscomplexTypegt ltxssequenc
egt ltxselement ref"FirstName"
/gt ltxselement ref"LastName"
/gt ltxselement ref"Age" /gt lt/xssequencegt
lt/xscomplexTypegt lt/xselementgt ltxselement
name"customers"gt ltxscomplexTypegt ltxssequen
cegt ltxselement ref"customer"
maxOccurs"unbounded" /gt lt/xssequencegt lt/xs
complexTypegt lt/xselementgt lt/xsschemagt
14
lt?xml version"1.0" encoding"UTF-8"
?gt ltcustomersgt ltcustomergt
ltFirstNamegtDwightlt/FirstNamegt
ltLastNamegtMercerlt/LastNamegt ltAgegt32lt/Agegt
lt/customergt ltcustomergt
ltFirstNamegtMiguellt/FirstNamegt
ltLastNamegtCabreralt/LastNamegt ltAgegt33lt/Agegt
lt/customergt lt/customersgt
15
Default XSD element types
  • xsString
  • xsnormalizedString
  • xsinteger
  • xsbase64Binary
  • xsnegativeInteger
  • xspositiveInteger
  • xsHexbinary
  • xsLong
  • xsshort

16
Custom Types
ltxsdsimpleType name"SKU"gt ltxsdrestriction
base"xsdstring"gt ltxsdpattern
value"\d3-A-Z2"/gt lt/xsdrestrictiongt
lt/xsdsimpleTypegt
17
XML Namespaces
  • Namespaces allow for XML to be segmented into
    different parts
  • ltnamespaceelementName /gt namespace will prefix
    the element name
  • ltfekkeroot xmlnsfekkehttp//www.fekke...
  • No namespace xmlns
  • Use explicit XPath with no namespace
  • Nonamespace xpath/element

18
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

19
StAX
  • BEA JSR 173
  • Many implementations
  • Includes Event and Pull parsers
  • Jim Collins working on CFStAX library
  • Woodstox library extremely fast implementation

20
StAX objects
  • XMLInputFactory
  • XMLStreamConstants
  • XMLStreamReader
  • XMLEventReader

21
XMLStreamConstants
  • Constants for different node types
  • XMLStreamConstants.START_ELEMENT
  • XMLStreamConstants.END_ELEMENT
  • XMLStreamConstants.START_DOCUMENT
  • XMLStreamConstants.END_DOCUMENT

22
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

23
Free XML Tools
  • IE and Firefox have built in DOM parsers
  • XMLFox for testing validation
  • XMLButterfly for editing and formatting XML
  • Dont use ALtova XML Spy, its crap!

24
Summary
  • Use XMLSearch instead of iterating through DOM
  • Dont 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

25
Links
  • http//www.w3.org/XML/
  • http//www.w3.org/XML/Schema
  • http//www.xml.com/
  • http//coldfusion.sys-con.com/read/236002.htm

26
Books
  • ColdFusion Application Development
  • Beginning XML (Programmer to Programmer)
  • Oreilly XML in a Nutshell
Write a Comment
User Comments (0)
About PowerShow.com