splitText Method

MSXML 5.0 SDK

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

splitText Method

Splits this text node into two text nodes at the specified offset and inserts the new text node into the tree as a sibling that immediately follows this node.

[Script]

Script Syntax

var objXMLDOMText = oXMLDOMText.splitText(offset);

Parameters

offset
A long integer. The number of characters at which to split this text node into two nodes, starting from zero.

Return Value

Typically, Returns the new DOM text node as an object. See Remarks for additional information.

Example

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

Visual Basic Syntax

Set objXMLDOMText = oXMLDOMText.splitText(offset)

Parameters

offset
A long integer. The number of characters at which to split this text node into two nodes, starting from zero.

Return Value

Typically, Returns the new DOM text node as an object. See Remarks for additional information.

Example

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

C/C++ Syntax

HRESULT splitText(
    long offset,
    IXMLDOMText **rightHandTextNode);

Parameters

offset [in]
The number of characters at which to split this text node into two nodes, starting from zero.
rightHandTextNode [out, retval]
The new text node.

C/C++ Return Values

S_OK
The value returned if successful.
S_FALSE
The value when returning Null.
E_FAIL
The value returned if an error occurs.

Typically, Returns the new DOM text node as an object. See Remarks for additional information.

C/C++ Example

IXMLDOMText *pIXMLDOMTextTemp = NULL;
IXMLDOMText *pIXMLDOMText = NULL;
IXMLDOMNode *pIXMLDOMNodeTemp = NULL;
IXMLDOMElement *pIXMLDOMElement = NULL;
IXMLDOMNode *pIXMLDOMNode = NULL;
DOMNodeType DOMNodeTyp;
VARIANT varValue;
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->QueryInterface(IID_IXMLDOMNode, (void
            **)&pIXMLDOMNode);
      SUCCEEDED(hr) ? 0 : throw hr;

      pIXMLDOMElement->Release();
      pIXMLDOMElement = NULL;

      while(pIXMLDOMNode)
      {
         hr = pIXMLDOMNode->get_nodeType(&DOMNodeTyp);
         if(SUCCEEDED ( hr) && (DOMNodeTyp == NODE_TEXT))
         {
            hr = pIXMLDOMNode->QueryInterface(IID_IXMLDOMText,
                     (void**)&pIXMLDOMText);
            SUCCEEDED(hr) ? 0 : throw hr;

            if(pIXMLDOMText)
            {
            hr = pIXMLDOMText->splitText(2, &pIXMLDOMTextTemp);
            if(SUCCEEDED(hr) && pIXMLDOMTextTemp)
            {
               hr = pIXMLDOMTextTemp->get_nodeValue(&varValue);
               if(SUCCEEDED(hr))
                  ::MessageBox(NULL, _bstr_t(varValue), _T("Notation Public ID"), MB_OK);
               pIXMLDOMTextTemp->Release();
               pIXMLDOMTextTemp = NULL;
            }
            pIXMLDOMText->Release();
            pIXMLDOMText = NULL;
         }
            pIXMLDOMNode->Release();
            pIXMLDOMNode = NULL;
            break;
         }
         pIXMLDOMNode->get_firstChild(&pIXMLDOMNodeTemp);
         pIXMLDOMNode->Release();
         pIXMLDOMNode = pIXMLDOMNodeTemp;
         pIXMLDOMNodeTemp = NULL;
      }
   }
}
catch(...)
{
   if(pIXMLDOMNodeTemp)
      pIXMLDOMNodeTemp->Release();
   if(pIXMLDOMNode)
      pIXMLDOMNode->Release();
   if(pIXMLDOMTextTemp)
      pIXMLDOMTextTemp->Release();
   if(pIXMLDOMText)
      pIXMLDOMText->Release();
   if(pIXMLDOMElement)
      pIXMLDOMElement->Release();
   DisplayErrorToUser();
}
// Release pIXMLDOMDocument when finished with it.

Remarks

The following table describes the effect of specifying various arguments in the offset parameter when using the splitText

If the value of offset is ... The result (HRESULT) will be ... The variable used to receive output (newNodeText, pXMLNode) will ...
A negative number An error (E_INVALIDARG) occurs. Not be set
Zero (0) A value of False (S_FALSE) is returned. Contain a null value (NULL)
Between 1 and (length - 1 of first DOM text node) Success (S_OK) Contain a second DOM text node, created as a sibling to the first node.
Equal to length of the first DOM text node A value of False (S_FALSE) is returned. Contain a null value (NULL)
Greater than the length of the first DOM text node An error (E_INVALIDARG) occurs. Contain a null value (NULL)

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

Applies to: IXMLDOMCDATASection | IXMLDOMText