The XML document object

ColdFusion represents an XML document as an object, called an XML document object, that is much like a standard ColdFusion structure. In fact, most ColdFusion structure functions, such as StructInsert, work with XML document objects. For a full list of ColdFusion functions that work on XML document objects, see Functions for XML object management.

You can look at the overall structure of an XML document in two ways: a basic view and a DOM (Document Object Model)-based node view. The basic view presents all the information in the document, but does not separate the data into as fine-grained units as the node view. ColdFusion can access XML document contents using either view.

A simple XML document

The descriptions of the basic and node views use the following simple XML document. This document is used in many of the examples in the ColdFusion XML documentation.

<?xml version="1.0" encoding="UTF-8"?> 
<employee> 
<!-- A list of employees --> 
    <name EmpType="Regular"> 
        <first>Almanzo</first> 
        <last>Wilder</last> 
    </name> 
    <name EmpType="Contract"> 
        <first>Laura</first> 
        <last>Ingalls</last> 
    </name> 
</employee>

Basic view

The basic view of an XML document object presents the object as a container that holds one root element structure. The root element can have any number of nested element structures. Each element structure represents an XML tag (start tag/end tag set) and all its contents; it can contain additional element structures. A basic view of the simple XML document looks like the following:

DOM node view

The DOM node view presents the XML document object using the same format as the document’s XML Document Object Model (DOM). In fact, an XML document object is a representation of a DOM object.

The DOM is a World Wide Web Consortium (W3C) recommendation (specification) for a platform- and language-neutral interface to dynamically access and update the content, structure, and style of documents. ColdFusion conforms to the DOM Level 2 Core specification, available at www.w3.org/TR/DOM-Level-2-Core.

In the DOM node view, the document consists of a hierarchical tree of nodes. Each node has a DOM node type, a node name, and a node value. Node types include Element, Comment, Text, and so on. The DOM structures the document object and each of the elements it contains into multiple nodes of different types, providing a finer-grained view of the document structure than the basic view. For example, if an XML comment is in the middle of a block of text, the DOM node view represents its position in the text while the basic view does not.

ColdFusion also lets you use the DOM objects, methods, and properties defined in the W3C DOM Level 2 Core specification to manipulate the XML document object.

For more information on referencing DOM nodes, see XML DOM node structure. This document does not cover the node view and using DOM methods and properties in detail.

XML document structures

An XML document object is a structure that contains a set of nested XML element structures. The following image shows a section of the cfdump tag output for the document object for the XML in A simple XML document. This image shows the long version of the dump, which provides complete details about the document object. Initially, ColdFusion displays a short version, with basic information. Click the dump header to change between short, long, and collapsed versions of the dump.

The following code displays this output. It assumes that you save the code in a file under your web root, such as C:\Inetpub\wwwroot\testdocs\employeesimple.xml

<cffile action="read" file="C:\Inetpub\wwwroot\testdocs\employeesimple.xml" 
    variable="xmldoc"> 
<cfset mydoc = XmlParse(xmldoc)> 
<cfdump var="#mydoc#">

The document object structure

At the top level, the XML document object has the following three entries:

Entry name

Type

Description

XmlRoot

Element

The root element of the document.

XmlComment

String

A string made of the concatenation of all comments on the document, that is, comments in the document prologue and epilog. This string does not include comments inside document elements.

XmlDocType

XmlNode

The DocType attribute of the document. This entry only exists if the document specifies a DocType. This value is read only; you cannot set it after the document object has been created

This entry does not appear when the cfdump tag displays an XML element structure.

The element structure

Each XML element has the following entries:

Entry name

Type

Description

XmlName

String

The name of the element; includes the namesapce prefix.

XmlNsPrefix

String

The prefix of the namespace.

XmlNsURI

String

The URI of the namespace.

XmlText or

XmlCdata

String

A string made of the concatenation of all text and CData text in the element, but not inside any child elements. When you assign a value to the XmlCdata element, ColdFusion places the text inside a CDATA information item. When you retrieve information from document object, these element names return identical values.

XmlComment

String

A string made of the concatenation of all comments inside the XML element, but not inside any child elements.

XmlAttributes

Structure

All of this element’s attributes, as name-value pairs.

XmlChildren

Array

All this element’s children elements.

XmlParent

XmlNode

The parent DOM node of this element.

This entry does not appear when the cfdump tag displays an XML element structure.

XmlNodes

Array

An array of all the XmlNode DOM nodes contained in this element.

This entry does not appear the cfdump tag when displays an XML element structure.

XML DOM node structure

The following table lists the contents of an XML DOM node structure:

Entry name

Type

Description

XmlName

String

The node name. For nodes such as Element or Attribute, the node name is the element or attribute name.

XmlType

String

The node XML DOM type, such as Element or Text.

XmlValue

String

The node value. This entry is used only for Attribute, CDATA, Comment, and Text type nodes.

Note: The cfdump tag does not display XmlNode structures. If you try to dump an XmlNode structure, the cfdump tag displays “Empty Structure.”

The following table lists the contents of the XmlName and XmlValue fields for each node type that is valid in the XmlType entry. The node types correspond to the objects types in the XML DOM hierarchy.

Node type

XmlName

xmlValue

CDATA

#cdata-section

Content of the CDATA section

COMMENT

#comment

Content of the comment

ELEMENT

Tag name

Empty string

ENTITYREF

Name of entity referenced

Empty string

PI (processing instruction)

Target entire content excluding the target

Empty string

TEXT

#text

Content of the text node

ENTITY

Entity name

Empty string

NOTATION

Notation name

Empty string

DOCUMENT

#document

Empty string

FRAGMENT

#document-fragment

Empty string

DOCTYPE

Document type name

Empty string

Note: Although XML attributes are nodes on the DOM tree, ColdFusion does not expose them as XML DOM node data structures. To view an element’s attributes, use the element structure’s XMLAttributes structure.

The XML document object and all its elements are exposed as DOM node structures. For example, you can use the following variable names to reference nodes in the DOM tree that you created from the XML example in A simple XML document:

mydoc.XmlName 
mydoc.XmlValue 
mydoc.XmlRoot.XmlName 
mydoc.employee.XmlType 
mydoc.employee.XmlNodes[1].XmlType