normalize Method

 

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

JScript Syntax

oXMLDOMElement.normalize();  

Example

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

Output

3  
1  

C/C++ Syntax

HRESULT normalize(void);  

Return Values

S_OK
The value returned if successful.

E_FAIL
The value returned if an error occurs.

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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0.

See Also

IXMLDOMText
IXMLDOMElement