Chapter 8 Working with XML Documents
This section describes how to use the DOM interface to create and edit XML documents.
You can output XML data from a database using standard string functions. The DOM interface lets you construct or modify the DOM representation of an XML document. To do anything with this document, you must process it using the DOM interfaces described in the other sections of this chapter.
The DOMDocument object has a constructor that allows you to create an empty DOM document.
To create a DOMDocument object:
domDoc = new DOMDocument();
The DOM Level 1 specification does not provide
a standard method for creating a DOM document object. The DOMDocument()
constructor
is an extension to the standard.
To add elements to a DOM document, create the elements or other object you want to add, and then insert them in the proper place in the tree.
You can create an element or other object using
methods of the DOMDocument
object.
These methods are:
The following fragment creates an FName element
(elem
), and a text node (tnode
)
with content "Ann T. Dote" in a DOMDocument
object
named domDoc:
elem = domDoc.createElement( "FName" );
tnode = domDoc.createTextNode( "Ann T. Dote" );
These nodes currently have no place in the document, and the FName element has no content.
You can add objects to a node using one of these methods of the DOMNode object:
This fragment expands on the previous one to
add the elements into a document. The FName element
is a child of a node named cust
,
and the text node is a child of the FName element:
elem = domDoc.createElement( "FName" );
text = domDoc.createTextNode("Ann T. Dote" );
el = cust.appendChild( elem );
el.appendChild( text );
For more information, see "insertBefore method" and "replaceChild method" in PowerDynamo Reference.
The DOM interface provides a set of methods
for deleting and replacing objects in the document tree. There is
not a method on a DOMNode
object
to delete the object itself. Instead, you delete a node by a method
on its parent. The removeChild
method
deletes a child node, and the replaceChild
method
replaces a child node with a new node.
For more information, see "removeChild method" in PowerDynamo Reference, and "replaceChild method" in PowerDynamo Reference.
For attributes and other unordered objects
(not elements) you can also use methods defined on the collection
of attributes. This collection, which is unordered, is represented
by a DOMNamedNodeList
object,
and you can use the removeNamedItem
method
and the setNamedItem
method
to carry out the operations.
For more information, see "removeNamedItem method" and "setNamedItem method" in PowerDynamo Reference.
Copyright © 1999 Sybase, Inc. All rights reserved. |