DOMDocument object

The DOMDocument object represents an XML document.

Creating a new XML document

Use the new keyword to create a new DOMDocument object.

{Create a new XML document.}
xmlDoc = new MSXML.DOMDocument("MSXML.DOMDocument");

When using Distributed COM (DCOM), create a new DOMDocument object with the COM_CreateObject() function. In the following example, the COM_CreateObject() function creates a new DOMDocument object on the computer “SystemServer”.

{Create a new XML document.}
xmlDoc = COM_CreateObject("MSXML.DOMDocument", "SystemServer");

Loading an existing XML document

Use the loadXML method to load a well-formed string of XML as an XML document. The loadXML method returns true if the XML string loads successfully. Otherwise, the method sets the documentElement property to null and returns false.

{Load a string of XML.}
loaded = xmlDoc.loadXML ("<seller><ID>20</ID><name>Don Hall</name></seller>");

You can use the load method to load an XML file. The load method returns true if the XML file loads successfully. Otherwise, the method sets the documentElement property to null and returns false. You can supply either the pathname or the URL of the XML file.

{Load an XML file.}
loaded = xmlDoc.load("C:\RESM\HOUSES.XML");

The loadXML method and the load method discard the current document before loading a new document. If the method fails, the documentElement property returns null.


Setting and retrieving the document element

The DOMDocument object can reference one IXMLDOMElement object, called the document element. You can set the document element with the documentElement property from the DOMDocument object.

{Set the document element.}
xmlDoc.documentElement = element;

You can also retrieve the document element with the documentElement property. If a document element does not exist, the property returns null.

{Retrieve the document element.}
element = xmlDoc.documentElement;

Specifying a document’s selection language

Use the setProperty method to specify the selection language for an XML document. The selection language allows you to search a document for a certain element. You must specify both the string “SelectionLanguage” and either the XSLPattern or XPath selection language. The XSLPattern language is the default selection language for XML documents.

{Specify XPath as the selection language for the document.}
xmlDoc.setProperty("SelectionLanguage", "XPath");

Refer to the help file for the Microsoft XML Object Library for more information on the XSLPattern and XPath selection languages.

Representing an XML document as text

Use the xml property to create a text representation of an XML document.

{Represent an XML document as text.}
XMLTextField = xmlDoc.xml;

Copying an XML document

Use the save method to copy an XML document to another DOMDocument object.

{Copy an XML document.}
xmlDoc.save(copiedDoc);

Saving an XML document

To save an XML document as a file, use the save method. You must supply a string containing the complete pathname for the file.

{Save the XML document as HOUSES.XML.}
xmlDoc.save("C:\RESM\HOUSES.XML");


Documentation Feedback