Getting and Setting Data Within a Node

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Getting and Setting Data Within a Node

Data marked up as text within elements, attributes, comments, and processing instructions is exposed in the Document Object Model (DOM) as node values.

The following code sets the value of an attribute to "hello world".

newAttNode = myElementNode.createAttribute("newAtt")
newAttNode.nodeValue = "hello world"

There are three ways to access text within an element node.

  • The nodeValue property provides direct access to values of certain types of nodes, including attributes, text nodes, comments, processing instructions, and CDATA sections. Navigate down to the element's children (the text nodes within) and call nodeValue.
  • The text property returns the text within the element. The text within an element can also be changed using the text property, but this will overwrite any child elements.

    The following sample code tests to see if the text within the element elem1 is equal to "hello world". If so, the element is assigned the text "hi! world".

    if (elem1.text == "hello world")
      elem1.text = "hi! world"

  • The nodeTypedValue property is part of the data type support built into Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office and returns the typed value of the node. This property is only for typed elements. The dataType property, which returns the data type of the element, can be used in conjunction with this property. The nodeTypedValue and datatype properties can both be set. However, the parser will return an error if the new nodeTypedValue is of the wrong data type or if the new data type does not match the nodeTypedValue. For more information about data type support, see XDR Schema Data Types Reference.

See Also

dataType Property | nodeValue Property | nodeTypedValue Property | text Property