CF And XML Tricks - PowerPoint PPT Presentation

About This Presentation
Title:

CF And XML Tricks

Description:

DOMAIN technologies. d.fauth_at_domain-tech.com. Knowledge ... Knowledge Engineering : Systems Integration : Web Development : Training. Why Do We Need XML? ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 36
Provided by: jacqueline80
Category:
Tags: xml | tricks

less

Transcript and Presenter's Notes

Title: CF And XML Tricks


1
CF And XML Tricks Dave Fauth DOMAIN
technologies d.fauth_at_domain-tech.com
Knowledge Engineering Systems Integration
Web Development Training
2
What Are We Covering?
  • Overview of XML
  • Why is XML important in web development?
  • Where is XML being used in Allaire products?
  • What techniques are available for processing XML
    from within ColdFusion?
  • Samples using several currently available custom
    tags/available products

3
What Is XML?
  • Extensible markup language
  • A world wide web consortium (W3C) standard that
    lets you create your own markup tags
    (www.W3.Org/xml/)
  • XML is a system for storing and presenting
    structured information on the web
  • XML contains data, structure and semantics, not
    formatting

4
Why Do We Need XML?
  • HTML is great but it is meant for browsing

lth3gtNumber of Sales Calls This Yearlt/h3gt lth2gtNorth
east Regionlt/h2gt lttablegtlttrgtlttdgtNew
Yorklt/tdgtlttdgtJanuarylt/tdgtlttdgt3,000lt/tdgtlt/trgt lttrgtlt
td colspan"2" align"right"gtFebruarylt/tdgtlttdgt14,0
00lt/tdgtlt/trgt lt/tablegt
  • It is hard to extract the data into any
    meaningful form.
  • Text parsing is slow and not an exact science.
  • Locked in to a specific delivery.

5
Who Is Using XML?
  • Microsoft Word 2000 save as web saves XML
    information
  • Microsoft SQL server 2000 will serve XML from the
    database
  • Oracle 8 allows for XML transformation within the
    database
  • Macromedia flash 5 reads native XML files
  • Microsoft commerce server 2000 uses XML
    extensively

6
XML Is Data Definition
  • XML Describes Data
  • This example is from Jrun Studio 3.0 help
  • Because XML describes data, the data can be
    stored anywhere, in many different formats and be
    delivered in many different formats

lt?xml version"1.0"?gt lthelp_refgt lthelp_book
path"Allaire_Support"gtAllaire Support lthelp_pagegt
jrstudio_release_notes.htmlt/help_pagegt lthelp_pagegt
AllaireProductSupport.htmlt/help_pagegt lthelp_pagegtA
llaireDeveloperResources.htmlt/help_pagegt lthelp_pag
egtVeritySearchTips.htmlt/help_pagegt lt/help_bookgt lt/
help_refgt
7
Easy to Transmit
  • XML is plain text, so it can be sent through a
    variety of protocols
  • HTTP
  • FTP
  • No additional ports need to be opened through a
    firewall

8
XML Is Well-formed
  • In order to make the data accessible, XML has to
    be well-formed
  • Attributes must be unique within an element
  • Attribute values must not contain the character
    "gt"
  • Every begin tag must have an end tag Or
  • Empty tags (without an end tag) must use the
    empty tag syntax
  • ltAuthor/gt
  • XML documents that are not well-formed will cause
    an exception in the XML processor
  • HTML does not have to be well-formed

9
XML May Be Validated
  • An XML file may be connected to a DTD (document
    type definition) which defines the document's
    validity constraints.
  • A validating XML processor is able to determine
    whether an XML file is valid according to the
    rules of the DTD.
  • An XML file must be well-formed to be valid, but
    it does not have to be valid to be well-formed.

lt?xml version"1.0"?gt lt!DOCTYPE HelpGuide SYSTEM
jrunhelp.dtd"gt lthelp_bookgt ... lt/help_bookgt
10
What Is WDDX?
  • Web distributed data exchange is an XML
    vocabulary with strong support in ColdFusion and
    other web development platforms (PHP, flash, ASP,
    Perl)
  • A free, open XML-based technology that allows web
    applications created with any platform to easily
    exchange data with one another over the web
  • Designed to store complex data objects such as
    arrays, structures and datasets
  • More information at WWW.WDDX.Org

11
Processing XML From ColdFusion
  • These parsing methods require a third-party XML
    processor
  • Extensible Stylesheet language transformation
    (XSLT) uses Stylesheet templates to transform XML
    data into formatted HTML.
  • The document object model (DOM) sees XML as a
    hierarchical tree which can be traversed with
    appropriate commands.
  • Simple API for XML (SAX) is an event-driven API
    for parsing large XML files which don't fit
    easily into server memory. SAX is only available
    in compiled languages due to need for "call-back"
    methods.

12
JAVA Based Processing
  • IBM LotusXSL java package
  • Java-based, can be used with the ltCFOBJECTgt tag
  • Requires xml4j/Xerces package
  • Good performance, requires more resources than MS
    COM object
  • Java language model
  • Cross-platform
  • Manual configuration
  • Download from http//www.alphaworks.ibm.Com/tech/
    LotusXSL

13
Where Do I Get Software
  • Download from xml.apache.org
  • Need Xerces, Xalan and LotusXSL software
  • Need JDK (1.3 recommended) (java.sun.com)

14
LotusXSL Configuration
  • Java Classpath in CF Administrator needs to
    include these JAR files
  • LotusXSL.jar
  • Xalan.jar
  • Xerces.jar
  • Java Runtime Environment (JRE) 1.2 or better must
    be configured in CF Administrator
  • Syntax with ltCFOBJECTgt tag

ltCFOBJECT TYPE"JAVA" ACTION"CREATE"
CLASS"com.lotus.xsl.XSLProcessor"
NAME"xslProcessor"gt
15
CF Server Configuration
16
XSLT Transforming XML Data
  • XSL is an advanced stylesheet language
    specifically designed for XML documents
  • XSLT provides the mechanism to translate from one
    XML document to another format
  • The new format can be text, html, xml, or almost
    any other format

17
XML Tranformation
  • Client-side
  • The XML and XSL documents are both served to an
    XML-aware web browser (internet explorer) which
    tranforms the document and presents it to the
    user
  • Server-side
  • The server applies an XSL document to the XML
    document and sends the resulting HTML code to the
    viewer (browser, WML, etc)
  • Another program (ColdFusion with an XML
    processor) performs the transformation and saves
    the resulting file to a static HTML file

18
Using XSL To Format Data
  • ltxsloutput method'html' /gt
  • ltxsltemplate match"/invoice"gt
  • lthtmlgtltheadgtlttitlegtHello
    Worldlt/titlegtlt/headgtltbodygt
  • lttablegt
  • ltxslapply-templates/gt
  • lt/tablegt
  • lt/bodygtlt/htmlgt
  • lt/xsltemplategt
  • XSL Commands are combined with HTML elements to
    create the desired output.

19
Using XSL to Format Data
  • The xslvalue-of command retrieves the data from
    the XML file.
  • "." Indicates current Element's nodeValue.
  • "_at_" indicates an Attribute name.
  • Note the "empty tag" syntax of the ltBR/gt tag.

ltxsltemplate match"equity-quote"gt
ltxslvalue-of select"."/gt, ltigtltxslvalue-of
select"_at_symbol"/gtlt/igt ltbr/gt lt/xsltemplategt
20
Transforming in the Browser
  • Add a stylesheet declaration to the XML file
  • Open in an XML-compatible browser
  • Internet explorer 5.X is an XML-compliant browser
    in general release
  • Netscape 6 reported to be compatible

lt?xml version"1.0"?gt lt?xml-stylesheet
type"text/xsl" hrefnasdaq.xsl"?gt ltequity-quotegt
... lt/equity-quotegt
21
Transforming Server Side with Java
  • ColdFusion Syntax using the ltCFOBJECTgt command
    with the LotusXSL processor

lt!--- Create the parser ---gt ltCFOBJECT
CLASS"com.lotus.xsl.XSLProcessor" TYPE"Java"
ACTION"Create" NAME"xslProcessor"gt lt!---
Process the xml file ---gt ltCFSET
currentDir GetDirectoryFromPath(GetCurrentTempla
tePath())gt ltCFSET xslProcessor.process(
"currentDirnasdaqallaire.xml",
"currentDir nasdaqallaire.xsl",
"currentdir nasdaqallaire.html")gt
22
XML Processors - MSXMLDOM
  • Microsoft XMLDOM COM object
  • A COM object which can be used from the
    ltCFOBJECTgt tag
  • Fast performance
  • VBScript language model
  • Windows specific
  • Version 2.0 built into MS internet explorer 5.X
  • New version 3.0 available for download
  • Automated installation for other systems
  • Download from
  • Http//msdn.Microsoft.Com/xml/
  • General/xmlparser.Asp

23
XMLDOM Traversing the Tree
  • DOM processing always occurs server-side
  • The XML document does not have to be validated
  • DOM interface has properties and methods designed
    to search for specific nodes of the tree by their
    names

24
XMLDOM Methods and Properties
  • Async. Returns control to the caller before the
    download is finished. In CF, set it to false.
  • Load(url). Loads the XML tree from a web
    document. url can include a file or http path.
  • selectSingleNode(nodename). Returns a single
    node matching nodename.
  • getElementsByTagName(nodename). Returns a
    collection of document-level nodes matching
    nodename.
  • selectNodes(nodename). Returns a collection of
    the current nodes children matching nodename.

25
XMLDOM Methods and Properties
  • childNodes(). Returns a collection of all of the
    nodes child nodes.
  • hasChildNodes(). Returns true if the node has
    child nodes.
  • NodeName. A property containing the name of the
    node (ex todays-high-price).
  • Text. A property containing the value between
    the begin and end tags (ex 6.375).
  • Attributes. A property returning a collection of
    attributes, each of which consists of a key-value
    pair.

26
Opening the XML File
  • 1. Instantiate the XMLDOM COM object.
  • 2. Get the complete filename based on the
    location of the current CF template.
  • 3. Set Async property to false so file load is
    single-threaded.
  • 4. Load the XML file into memory.
  • 5. Perform an error check for an empty node tree.

1) ltcfobject action"create" type"COM"
class"Microsoft.XMLDOM" name"Parser"gt 2) ltcfset
xmlFile nasdaqallaire.xml"gt 3) ltcfset
Parser.Async falsegt 4) ltcfset
Parser.Load(xmlFile)gt 5) ltcfif not
parser.hasChildNodes()gt ltcfthrow
message"Empty XML Tree"gt lt/cfifgt
27
Reading Individual Values
  • 1. Use selectSingleNode() to load the entire
    timesheet" into a node object.
  • Use the same method to load the company and state
    tags and create objects for them.
  • Use the text property to retrieve the company and
    state values.

ltcfscriptgt timesheet XMLDoc.selectSingle
Node("timesheets") nodeCompany
timesheet.selectSingleNode("company") Company
nodeCompany.Text nodeState
timesheet.selectSingleNode("state") State
nodeState.Text lt/cfscriptgt
28
Getting a Collection of Nodes
  • Use selectSingleNode() to load employees Node.
  • Use ChildNodes to load list of employees.
  • Create a CF Array to hold the employee data.

ltCFSET empstimesheet.selectSingleNode("employees"
)gt ltCFSET empsemps.ChildNodesgt ltCFSET
aEmpsArraynew(1)gt
29
Getting a Collection of Nodes
  • Use ltCFLOOPgt to iterate through the node list.
  • Place attributes into a CF structure using an
    internal loop through the attributes property.

ltCFLOOP collection"emps" item"emp_name"gt ltCF
SET ArrayAppend(aEmps, structnew())gt ltCFSET
iarrayLen(aEmps)gt ltCFSET attributesemp_name.att
ributesgt ltCFLOOP collection"attributes"
item"attribute"gt ltCFSET aEmpsiattribute.nod
ename attribute.textgt lt/CFLOOPgt lt/CFLOOPgt
30
Using the XML Data
  • Output to HTML

ltCFOUTPUTgt ltH2gtCompanylt/H2gt ltH3gtStatelt/H3gt
ltPgtltBgtEmployee Listlt/Bgtlt/Pgt ltCFLOOP from"1"
to"arrayLen(aEmps)" index"i"gt aEmpsi.depar
tment, ltigtaEmpsi.supervisorlt/igtltbrgt
lt/CFLOOPgt lt/CFOUTPUTgt
31
Using the XML Data
  • Output to CF Structure

ltCFSET employeestruct structnew()gt ltCFSET
XMLDoc.company companygt ltCFSET XMLDoc.state
stategt ltCFSET XMLDoc.aEmps aEmpsgt ltCFWDDX
action"CFML2WDDX" input"XMLDoc"
output"WDDXDoc"gt
32
CFX Tags for XML Transform
  • CFX_parser
  • CFX_Jxpathl
  • Obtained from www.plog.net/cfx_parser
  • Java tags for parsing and doing Xpath queries
  • Xpath searches and retrieves the XML file for
    certain element(s) of the tree
  • Example /doc/name2/_at_first

33
Custom Tags
  • SOXML
  • Custom tag from www.siteobjects.com
  • ltcf_SOXML
  • action"CF2XMLDomCF2XMLXML2CFXML2DOMXML2HTML
    "
  • input"" requiredall actions
  • output"" requiredCF2XMLDomCF2XMLXML2CFXML2
    DOM
  • type"" requiredXML2CFXML2DOM
  • rootname"" requiredCF2XMLDomCF2XML
  • progid"" optional
  • gt
  • Heads Up The tag relies on connecting to the
    Internet for the XSL file. You will need to
    change that for internal use.

34
Summary
  • XML is wide-spread as a means of storing and
    transmitting data in a structured text format
  • Third-party XML processors can be used with
    ColdFusion to parse pre-existing XML data
  • Both the DOM and XSLT interfaces can be used
    depending on the specific application needs

35
Resources
  • http//Msdn.microsoft.com/xml
  • http// www.siteobjects.com
  • http//www.ibm.com/developer/xml/
  • http//www.xmlhack.org
  • http//www.xmlmag.com/ (free subscription)
  • http//www.xml.com/
  • http//apache.xml.org/
Write a Comment
User Comments (0)
About PowerShow.com