Chapter 8 Working with XML Documents
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).
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:
toDOMDocument
function parses
a string that contains XML data. It builds an internal representation
of the XML document so that it can be manipulated and acted on.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.
In a DOM representation of this document, each element is a node. You can navigate the document using the relationships among these nodes:
parentNode
property
that takes you to the parent of the current node.childNodes
property
that provides you with a list of all children of the current node.
It also provides a firstChild
and lastChild
node
to take you to those specific child nodes. These nodes are ordered,
and each has a previousSibling
property
and a nextSibling
property
that enable you to loop through the current node.nodeType
of
Text and a nodeValue
of Gagliardo
.<Sentence>The cat was <Em>very</Em> black</Sentence>
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:
nodeName
, nodeType
,
and nodeValue
properties give
information about the node. For example, nodeType
tells
you whether the node represents an element, attribute, text, or
some other node type.parentNode
, previousSibling
, nextSibling
, firstChild
,
and lastChild
properties
to take you to related nodes. You can use the hasChildNodes()
method
to determine if a node has children, and if it does, the childNodes
property
returns a whole list of children nodes.insertBefore
, replaceChild
, removeChild
, appendChild
,
and cloneNode
to add or
remove related nodes.The properties of nodes depend on what piece of an XML document they represent. The node types and their corresponding values are:
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.
Performance tip
It is generally more efficient to
use the explicit objects instead of the generic node interface to
XML documents.
To assist in developing reliable scripts Dynamo provides:
DOMNode
object
provides a prettyPrint()
method
that returns the XML tree starting at the current node. This method is
an extension to the DOM recommendation, and takes no arguments.
You can use it in a statement such as:document.writeln( myNode.prettyPrint() );
DOMNode.appendChild()
returns
the child node. On failure, the return value is null.
You can compare the return value to null to check
for the success of a method.toDOMDocument
returns null.
You can use site.GetErrorInfo()
to
provide information on where the document is incorrect. The following
code fragment illustrates how to do this.domDoc = toDOMDocument( xmlString );
if( domDoc == null ) {
document.writeln( site.GetErrorInfo() );
}
else {
...
}
Copyright © 1999 Sybase, Inc. All rights reserved. |