Utilizing xml in coldfusion mx
Download
1 / 16

Utilizing XML in ColdFusion MX - PowerPoint PPT Presentation


  • 126 Views
  • Uploaded on

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 of ColdFusion, 5 months of CF MX experience PHP and JAVA enthusiast. Agenda. What is XML - the XML syntax

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Utilizing XML in ColdFusion MX' - ariadne


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Utilizing xml in coldfusion mx

Utilizing XML in ColdFusion MX

by Attila Domokos


Speaker information
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


Agenda
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


What is xml
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


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 (<!- - This is a comment - - >)

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

XML Syntax


An example
An Example

<?xml version=“1.0” encoding=“UTF-8”?>

<!- - This is my first comment - ->

<company name=“Macromedia”>

<location name=“San Francisco”>

<employees>

<person>Tom Cruise</person>

<person>Ben Forta</person>

<person>Nicole Kidman</person>

</employees>

</location>

</company>

Preview in Browser


Parsing the xml document
Parsing the XML Document

1. Specify the file and its path first <CFSET MyXMLFile = ExpandPath(“company.xml”)>

2. Read the XML file into a string variable <CFFILE ACTION=“READ” FILE=“MyXMLFile” VARIABLE=“MyXMLCode”>

3. Parse the document with XMLParse() function into an XML “object” <CFSET MyXML = XMLParse(MyXMLCode)> VIEW


Xml element objects i
XML Element Objects I.

One root element in XML documents <CFSET xnCompany = MyXml.XmlRoot>

  • XmlName - the name of the element <CFSET enCompany = nCompany.XmlName>

  • XmlAttributes – contains all the attributes of the corresponding element <CFSET sCompanyName = xnCompany.XmlAttributes[“name”]> VIEW


Xml element objects ii
XML Element Objects II.

  • XmlChildren – An array that contains all the immediate children of the element <CFSET Locations = xnCompany.XmlChildren>

    The number of child level elements can be determined with the length of the array

    <CFSET nNumLocations = ArrayLen(Locations)>

    Each item contained by this array will be another XML element object (has all three properties) VIEW


Looping through child elements
Looping Through Child Elements

To display all the locations in the example, you have to loop through the Locations array.

<CFLOOP FROM=1 TO=“#nNumLocations#” INDEX=“I”>

<CFSET xnThisLocation = Locations[i]>

<CFSET sLocationName = xnThisLocation.XmlAttributes[“name”]>

<CFOUTPUT><p>Locations #i# is: <b>#sLocationName#</b></p></CFOUTPUT>

</CFLOOP>

VIEW


Nested loops
Nested Loops

Use nested loops if you want to display the employee’s name under locations from the example (company.xml).

<CFSET arEmployees = xnThisLocation[“employees”].XmlChildren>

<CFSET nNumEmployees = ArrayLen(arEmployees)>

<CFLOOP FROM=1 TO=“#nNumEmployees#” INDEX=“j”>

<CFOUTPUT>#arEmployees[j].XmlText#</CFOUTPUT>

</CFLOOP>

VIEW


Xmlsearch and xpath
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


Find elements with xpath
Find Elements with XPath

XmlSearch(MyXml, “/company/location/employees/person”)

  • This code will find ALL the <person> elements

    VIEW

    XmlSearch(MyXml, “/company/location[@name=‘Newton’]/employees/person”)

  • You’ll get only the employees belonging to the “Newton” location VIEW

    XmlSearch(MyXml, “/company/location[@name=‘#URL.LocName#’]/employees/person”)

  • Location name passed through URL VIEW


Generating new xml docs
Generating New XML Docs

  • <CFXML> tag to output XML

    <CFXML VARIABLE=“MyXml”> XML code </CFXML>

  • 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 <CFFILE>, upload it with <CFFTP> or send it with <CFMAIL>

    VIEW


Real world example
Real World Example

Get the latest articles from Macromedia Designer & Developer Center’s 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 <CFHTTP> tag

    Let’s create this .cfm template together!


Credits
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


ad