Chapter 8 Working with XML Documents


Introduction to the Document Object Model

Dynamo provides a set of DynaScript objects for navigating and manipulating XML documents. These objects implement the Document Object Model (DOM) core Level 1 features. The DOM is a W3C recommendation (REC-DOM-Level-1-19981001).

What you can do with DOM

With the DOM interface, you can access and modify the content and structure of XML documents from DynaScript. Tasks that you can carry out using DOM including:

The DOM view of documents

The DOM interface presents XML documents to your application as a tree. Consider the following simple XML document, shown with indented lines for readability:

<?xml version='1.0' ?>
<Customer>
<FName>Jessie</FName>
<LName>Gagliardo</LName>
<Address>
<Street>2800 Park Avenue</Street>
<City>Hull</City>
<Region Type=Province>PQ</Region>
</Address>
</Customer>

The tree representation of this document is shown in Figure 8-1.

Figure 8-1: Tree representation of XML document

The node interface to DOM

In a DOM representation of this document, each element is a node. You can navigate the document using the relationships among these nodes:

Using nodes to work with XML documents

The DOMNode object is the primary object for the DOM model of documents. It has a set of properties that you can use to obtain information about the node:

Node types

The properties of nodes depend on what piece of an XML document they represent. The node types and their corresponding values are:

nodeType value

XML

Comments

1

Element

Many nodes are XML elements.

2

Attribute

Attributes of an XML element.

3

Text

Text can occur in elements, or in the values of attributes.

4

CDATASection

Typically used for including blocks of text that contain characters that would otherwise be regarded as markup (such as <, >).

5

EntityReference

A reference to an XML entity.

6

Entity

An XML entity.

7

ProcessingInstruction

An XML processing instruction.

8

Comment

An XML comment: all the characters between <!-- and --> .

9

Document

The document with which the node is associated.

10

DocumentType

The document type definition (DTD) of the document.

11

DocumentFragment

A piece of a document, primarily used for editing purposes.

12

Notation

An XML notation.

The object interface to XML documents

While you can access all parts of a document as nodes, the DOM interface also provides explicit objects for elements (DOMElement ), attributes (DomAttr ), and other node types. These objects inherit properties from the DOMNode object and provide some additional functionality of their own. The properties and methods of each of these objects reflect the features of the XML object they represent.

The following sections describe both the DOMNode approach and the object interface. Although you can mix these approaches, most developers will want to use one method per task, for consistency.

Note  

Performance tip
It is generally more efficient to use the explicit objects instead of the generic node interface to XML documents.

Error checking XML-handling scripts

To assist in developing reliable scripts Dynamo provides:

 


Copyright © 1999 Sybase, Inc. All rights reserved.