Inserts a child node to the left of the specified node or at the end of the list.
var objXMLDOMNode = oXMLDOMNode.insertBefore(newChild, refChild);
newChild parameter is inserted to the left of the refChild parameter. If Null, the newChild parameter is inserted at the end of the child list.Object. On success, returns the child node that was inserted.
The following script example creates a new IXMLDOMNode object and inserts it before the second child of the top-level node.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var root;
var newNode;
var currNode;
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
root = xmlDoc.documentElement;
alert(root.xml);
newNode = xmlDoc.createNode(1, "VIDEOS", "");
currNode = root.insertBefore(newNode, root.childNodes.item(1));
alert(root.xml);
}
Set objXMLDOMNode = oXMLDOMNode.insertBefore(newChild, refChild)
newChild parameter is inserted to the left of the refChild parameter. If Null, the newChild parameter is inserted at the end of the child list.An object. On success, returns the child node that was inserted.
The following Visual Basic example creates a new IXMLDOMNode object and inserts it before the second child of the top-level node.
Dim xmlDoc As New Msxml2.DOMDocument40
Dim root As IXMLDOMElement
Dim newNode As IXMLDOMNode
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.resolveExternals = False
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
MsgBox root.xml
Set newNode = xmlDoc.createNode(NODE_ELEMENT, "VIDEOS", "")
Set currNode = root.insertBefore(newNode, root.childNodes.Item(1))
MsgBox root.xml
End If
HRESULT insertBefore(
IXMLDOMNode *newChild,
VARIANT refChild,
IXMLDOMNode **outNewChild);
newChild parameter is inserted to the left of the refChild parameter. If Null, the newChild parameter is inserted at the end of the child list.newChild parameter is Null.This operation depends on the value of the nodeType property.
This operation depends on the value of the newChild parameter.
NODE_ATTRIBUTE, NODE_CDATA_SECTION, NODE_COMMENT, NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_ELEMENT, NODE_ENTITY, NODE_NOTATION, NODE_PROCESSING_INSTRUCTION |
Returns an error. These node types cannot be children of an attribute. |
NODE_DOCUMENT_FRAGMENT |
Inserts the children of the document fragment (newChild) and returns newChild. |
NODE_ENTITY_REFERENCE, NODE_TEXT |
Inserts newChild and returns newChild. |
NODE_CDATA_SECTION, NODE_COMMENT, NODE_ENTITY, NODE_NOTATION, NODE_PROCESSING_INSTRUCTION, NODE_TEXT |
Returns an error. These node types either cannot have children or their children are read-only. |
This operation depends on the value of the newChild parameter.
NODE_ATTRIBUTE, NODE_CDATA_SECTION, NODE_DOCUMENT, NODE_ENTITY, NODE_ENTITY_REFERENCE, NODE_NOTATION, NODE_TEXT |
Returns an error. These nodes are not valid as children of a document node. |
NODE_COMMENT, NODE_PROCESSING_INSTRUCTION |
Inserts newChild and returns newChild. |
NODE_ELEMENT |
Inserts newChild and returns newChild. By definition, an XML document (the Document node) can have only a single child. Therefore, an error is returned if the Document node already has a child. |
NODE_DOCUMENT_FRAGMENT |
Inserts the children of the document fragment (in newChild) and returns newChild. The insert operations are subject to the rules for child nodes and can fail if the document fragment children represent node types that cannot be inserted. |
NODE_DOCUMENT_TYPE |
Returns an error. The document type is read-only. |
This operation depends on the value of the newChild parameter.
NODE_ATTRIBUTE, NODE_DOCUMENT, NODE_DOCUMENT_TYPE |
Returns an error. These nodes types are not valid as children of a document fragment. |
NODE_CDATA_SECTION, NODE_COMMENT, NODE_ELEMENT, NODE_ENTITY_REFERENCE, NODE_PROCESSING_INSTRUCTION, NODE_TEXT |
Inserts newChild and returns newChild. |
NODE_DOCUMENT_FRAGMENT |
Inserts the children of the document fragment (newChild) and returns newChild. |
NODE_ENTITY, NODE_NOTATION |
Returns an error. Entities and notations are read-only and cannot be inserted into a document. |
This operation depends on the value of the newChild parameter.
NODE_CDATA_SECTION, NODE_COMMENT, NODE_ELEMENT, NODE_ENTITY_REFERENCE, NODE_TEXT, NODE_PROCESSING_INSTRUCTION |
Inserts newChild and returns newChild. |
NODE_ATTRIBUTE, NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_ENTITY, NODE_NOTATION |
Returns an error. These node types cannot be children of an element node. |
NODE_DOCUMENT_FRAGMENT |
Inserts the children of the document fragment (newChild) and returns newChild. |
NODE_ENTITY_REFERENCE |
Returns an error. Although the child nodes of an entity reference are the expanded entity, the children cannot be modified. |
The node supplied in the refChild parameter must be a child node of this node or Null. The newChild parameter is inserted before the refChild parameter (as the left sibling) in the child list. If refChild is Null, newChild is inserted at the end of the child list. If refChild is not a child of this node, an error is returned.
If newChild is a node of type DOCUMENT_FRAGMENT, all its children are inserted, in the same order, before refChild. If newChild is already in the tree, it is first removed.
These parameters can represent nodes in the same or different documents. When in the same document, the nodes retain their default attributes and data types. When these parameters represent nodes in different documents, the nodes either lose or alter their default attributes, depending on whether this node's document has a document type definition (DTD) or other schema. MSXML attempts to correctly merge the different DTDs.
If the newChild node's DTD or schema differs from the original document, the nodes will be treated with the definitions in the new DTD, including default attributes and data types. If there is no DTD, the nodes keep their data types by picking up an instance definition (attributes lose their data types), and default attributes are lost. Cutting and pasting between documents with two different DTDs can result in an invalid document that may fail to parse after being saved.
When elements are added as children to other elements, they introduce their own namespace scoping. That is, if the element is from a default namespace, the default has scope beginning with the element being added.
If an entity reference is being added as a child to an element or document fragment, the namespace declaration of the entity reference is added to the containing element. In a similar way, if the entity reference is being added to an attribute, the namespace and the prefix of the entity reference are added to the element containing the attribute.
If there is a conflict between prefixes on the containing element and the prefixes being added, the insertBefore operation fails and returns an error. For example, a conflict occurs when a new attribute referring to a different namespace is added to an element with the namespace, as in the following.
xmlns:myns="URN1"
The error can result from the new attribute, where myns refers to a different namespace, "URN2," such as would result from a call to createNode ("attribute","myns:myname","URN2").
myns:myname="myattributevalue"
Namespace conflict errors occur only when setting attributes. Inserted child elements do not cause a namespace conflict.
When adding a document fragment, the containing element adds all namespaces and prefixes from the document fragment. If this causes a conflict with the containing element, insertBefore returns an error.
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.
createNode Method | nodeType Property
Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText
| This HTML Help has been published using the chm2web software. |