normalize Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

normalize Method

Normalizes all descendant elements by combining two or more adjacent text nodes into one unified text node.

[Script]

Script Syntax

oXMLDOMElement.normalize();

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var nodeRoot;
xmlDoc.async = false;
xmlDoc.loadXML("<root/>");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   nodeRoot = xmlDoc.documentElement;
   nodeRoot.appendChild(xmlDoc.createTextNode("Hello "));
   nodeRoot.appendChild(xmlDoc.createTextNode("World"));
   nodeRoot.appendChild(xmlDoc.createTextNode("!"));
   alert(nodeRoot.childNodes.length);
   nodeRoot.normalize();
   alert(nodeRoot.childNodes.length);
}
[Visual Basic]

Visual Basic Syntax

oXMLDOMElement.normalize

Example

Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeRoot As IXMLDOMElement
xmlDoc.async = False
xmlDoc.loadXML "<root/>"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set nodeRoot = xmlDoc.documentElement
   nodeRoot.appendChild xmlDoc.createTextNode("Hello ")
   nodeRoot.appendChild xmlDoc.createTextNode("World")
   nodeRoot.appendChild xmlDoc.createTextNode("!")
   MsgBox nodeRoot.childNodes.length
   nodeRoot.normalize
   MsgBox nodeRoot.childNodes.length
End If
[C/C++]

C/C++ Syntax

HRESULT normalize(void);

C/C++ Return Values

S_OK
The value returned if successful.
E_FAIL
The value returned if an error occurs.

C/C++ Example

BOOL DOMElementNormalize()
{
   BOOL bResult = FALSE;
   IXMLDOMElement *pIXMLDOMElement = NULL;
   IXMLDOMDocument *pIXMLDOMDocument = NULL;
   HRESULT hr;
   
   try
   {
      // Create an instance of DOMDocument and initialize
      // pIXMLDOMDocument.
      // Load/create an XML fragment.
      hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
      SUCCEEDED(hr) ? 0 : throw hr;

      if(pIXMLDOMElement )
      {
         hr = pIXMLDOMElement->normalize();
         if(SUCCEEDED(m_hr))
            bResult = TRUE;
         pIXMLDOMElement->Release();
      }
   }
   catch(...)
   {
      if(pIXMLDOMElement)
         pIXMLDOMElement->Release();
      DisplayErrorToUser();
   }
   return bResult;
}

Remarks

This method converts all text node descendants of this element (at any depth) into a "normal" form where no text nodes are adjacent. In normal form, text nodes can be separated only by markup, such as tags, comments, processing instructions, CDATA sections, and entity references. The normal form is useful for operations that require a particular document tree structure and ensures that the XML Document Object Model (DOM) view of a document is identical when saved and reloaded.

Collapsed node objects are deleted automatically.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IXMLDOMText

Applies to: IXMLDOMElement