Utilizing XML in ColdFusion MX - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Utilizing XML in ColdFusion MX

Description:

Utilizing XML in ColdFusion MX by Attila Domokos Speaker Information Who am I? Attila Domokos Web Programmer at a Twin Cities based web development company 2+ years ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 17
Provided by: Atti6
Category:

less

Transcript and Presenter's Notes

Title: Utilizing XML in ColdFusion MX


1
Utilizing XML in ColdFusion MX
  • by Attila Domokos

2
Speaker Information
  • Who am I?
  • Attila Domokos
  • Web Programmer at a Twin Cities based web
    development company
  • 2 years of ColdFusion, 5 months of CF MX
    experience
  • PHP and JAVA enthusiast

3
Agenda
  • What is XML - the XML syntax
  • Parsing the XML document
  • XML element objects
  • Looping through child elements
  • XML search with XPath
  • Generating new XML documents
  • Real World Example

4
What is XML?
  • eXtensible Markup Language
  • XML looks a bit like HTML
  • XML offers a method of putting structured data
    into a text file
  • XML is machine-readable, human-intelligible
  • XML is license-free, platform-independent and
    well supported

5
XML Syntax
  • More strict than HTML
    (Proper nesting and closing tags are
    required.)
  • Starts with XML declarations
  • Comments take the same form as they do in HTML
    (lt!- - This is a comment - - gt)
  • DTD (Document Type Definition), XDR (XML Data
    Reduced Schema) and XSD (XML Schema Definition)
    are used for describing a document
  • Validation in IE 5 browsers

6
An Example
  • lt?xml version1.0 encodingUTF-8?gt
  • lt!- - This is my first comment - -gt
  • ltcompany nameMacromediagt
  • ltlocation nameSan Franciscogt
  • ltemployeesgt
  • ltpersongtTom Cruiselt/persongt
  • ltpersongtBen Fortalt/persongt
  • ltpersongtNicole Kidmanlt/persongt
  • lt/employeesgt
  • lt/locationgt
  • lt/companygt
  • Preview in Browser

7
Parsing the XML Document
  • 1. Specify the file and its path first
    ltCFSET MyXMLFile ExpandPath(company.xml)gt
  • 2. Read the XML file into a string variable
    ltCFFILE ACTIONREAD

    FILEMyXMLFile
    VARIABLEMyXMLCodegt
  • 3. Parse the document with XMLParse() function
    into an XML object ltCFSET MyXML
    XMLParse(MyXMLCode)gt

    VIEW

8
XML Element Objects I.
  • One root element in XML documents ltCFSET
    xnCompany MyXml.XmlRootgt
  • XmlName - the name of the element ltCFSET
    enCompany nCompany.XmlNamegt
  • XmlAttributes contains all the attributes of
    the corresponding element ltCFSET sCompanyName
    xnCompany.XmlAttributesna
    megt VIEW

9
XML Element Objects II.
  • XmlChildren An array that contains all the
    immediate children of the element ltCFSET
    Locations xnCompany.XmlChildrengt
  • The number of child level elements can be
    determined with the length of the array
  • ltCFSET nNumLocations ArrayLen(Locations)gt
  • Each item contained by this array will be
    another XML element object (has all three
    properties) VIEW

10
Looping Through Child Elements
  • To display all the locations in the example, you
    have to loop through the Locations array.
  • ltCFLOOP FROM1 TOnNumLocations INDEXIgt
  • ltCFSET xnThisLocation Locationsigt
  • ltCFSET sLocationName
    xnThisLocation.XmlAttributesnamegt
  • ltCFOUTPUTgtltpgtLocations i is
    ltbgtsLocationNamelt/bgtlt/pgtlt/CFOUTPUTgt
  • lt/CFLOOPgt
  • VIEW

11
Nested Loops
  • Use nested loops if you want to display the
    employees name under locations from the example
    (company.xml).
  • ltCFSET arEmployees xnThisLocationemployees.X
    mlChildrengt
  • ltCFSET nNumEmployees ArrayLen(arEmployees)gt
  • ltCFLOOP FROM1 TOnNumEmployees INDEXjgt
  • ltCFOUTPUTgtarEmployeesj.XmlTextlt/CFOUTPUTgt
  • lt/CFLOOPgt
  • VIEW

12
XMLSearch() and XPath
  • Querying an XML file is available with the
    XMLSearch() function through XPath
  • XPath is a way to apply XSLT stylesheets, now
    think of it as the XML equivalent to the WHERE
    clause in SQL query
  • XPath uses slashes to represent the nested
    elements

13
Find Elements with XPath
  • XmlSearch(MyXml, /company/location/employees/pers
    on)
  • This code will find ALL the ltpersongt elements
  • VIEW
  • XmlSearch(MyXml, /company/location_at_nameNewton
    /employees/person)
  • Youll get only the employees belonging to the
    Newton location VIEW
  • XmlSearch(MyXml, /company/location_at_nameU
    RL.LocName/employees/person)
  • Location name passed through URL VIEW

14
Generating New XML Docs
  • ltCFXMLgt tag to output XML
  • ltCFXML VARIABLEMyXmlgt
    XML code
    lt/CFXMLgt
  • Output the XML as a string use the ToString()
    function
  • Once you have the XML code as string, you can
    save it to disk with ltCFFILEgt, upload it with
    ltCFFTPgt or send it with ltCFMAILgt
  • VIEW

15
Real World Example
  • Get the latest articles from Macromedia
    Designer Developer Centers XML feed to your
    site!
  • The XML file can be found at
  • http//www.macromedia.com/desdev/resources/
  • macromedia_resources.xml
  • To read the file, use the ltCFHTTPgt tag
  • Lets create this .cfm template together!

16
Credits
  • Devan Shepherd - Teach Yourself XML in 21 days -
    (2001) - SAMS
  • David Hunter - Beginning XML (2000) - WROX
  • Utilizing XML and XSLT in ColdFusion MX - Nate
    Weiss (2002 April) -macromedia.com
Write a Comment
User Comments (0)
About PowerShow.com