IXMLDOMText
Represents the text content of an element or attribute.
Example
The following script example creates an IXMLDOMText
object (objText
), and inserts it before the first child of the root. The insertBefore
method returns another IXMLDOMText
object, objText1
.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); var root; var objText; var objText1; var objNode; xmlDoc.load("books.xml"); if (xmlDoc.parseError.errorCode != 0) { var myErr = xmlDoc.parseError; alert("You have error " + myErr.reason); } else { root = xmlDoc.documentElement; objText = xmlDoc.createTextNode("Hello World"); objText1 = root.insertBefore(objText, root.firstChild); alert(root.xml); }
Example
The following Microsoft® Visual Basic® example creates an IXMLDOMText
object (objText
), and inserts it before the first child of the root. The insertBefore
method returns another IXMLDOMText
object, objText1
.
Dim xmlDoc As New Msxml2.DOMDocument50 Dim root As IXMLDOMElement Dim objText As IXMLDOMText Dim objNode As IXMLDOMNode xmlDoc.Load ("books.xml") If (xmlDoc.parseError.errorCode <> 0) Then Dim myErr Set myErr = xmlDoc.parseError MsgBox("You have error " & myErr.reason) Else Set root = xmlDoc.documentElement Set objText = xmlDoc.createTextNode("Hello World") Set objText1 = root.insertBefore(objText, root.firstChild) MsgBox root.xml End If
Example
The following C/C++ example creates and appends a new text node to the root document element.
#import "msxml5.dll" using namespace MSXML2; inline void TESTHR( HRESULT _hr ) { if FAILED(_hr) throw(_hr); } void XMLDOMText() { try { IXMLDOMDocumentPtr docPtr; IXMLDOMNodePtr DOMNodePtr; IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr; //init TESTHR(CoInitialize(NULL)); TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.5.0")); // load a document _variant_t varXml("C:\\book.xml"); _variant_t varOut((bool)TRUE); varOut = docPtr->load(varXml); if ((bool)varOut == FALSE) throw(0); DOMNodePtr = docPtr->createTextNode("Hello World"); docPtr->documentElement->appendChild(DOMNodePtr); MessageBox(NULL, _bstr_t(docPtr->xml), _T("New Document"), MB_OK); } catch(...) { MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK); } CoUninitialize(); }
Remarks
XML refers to this text content as character data and distinguishes it from markup, the tags that modify that character data. If there is no markup inside an element, that element's text is contained in a single IXMLDOMText
object that is the child of the element. If there is markup inside an element, it is parsed into child elements that are siblings of the IXMLDOMText
object(s). (The content of the markup elements also appears within text nodes, which are the children of the specific markup element.)
When a document is first made available to the XML Document Object Model (DOM), all text is normalized: there is only one text node for each block of text. Users can create adjacent text nodes that represent the contents of a given element without any intervening markup but should be aware that there is no way to represent the separations between these nodes, so they will not persist between XML DOM sessions.
The normalize
method on the IXMLDOMElement
object merges adjacent text nodes into a single node. You should normalize before starting any operations that depend on a particular document structure to ensure that subsequent sessions will operate on the same structure.
Versioning
MSXML 2.0 and later
Requirements
Implementation: msxml5.dll, msxml2.lib
Header and IDL files: msxml2.h, msxml2.idl
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.
See Also
IXMLDOMElement | IXMLDOMText Members | normalize Method