Inserts a child node to the left of the specified node or at the end of the list.
Visual Basic Syntax
Set objXMLDOMNode = oXMLDOMNode.insertBefore(newChild, refChild)
C/C++ Syntax
HRESULT insertBefore(
IXMLDOMNode *newChild,
VARIANT refChild,
IXMLDOMNode **outNewChild);
Parameters
newChild
[in]
Address of the new node to be inserted.
refChild
[in]
Address of the reference node; newChild is inserted to the left of refChild. If NULL, newChild is inserted at the end of the child list.
outNewChild
[out]
On success, the child node that was inserted. If NULL, no object is created.
C/C++ Return Values
S_OK
Value returned if successful.
E_INVALIDARG
Value returned if newChild is null.
E_FAIL
Value returned if an error occurs.
Remarks
When moving a node, it retains its namespace information. Moving a node doesn't create a namespace declaration, but declarations are added when retrieving the XML source (via the IXMLDOMDocument::save or IXMLDOMNode::get_xml methods) to ensure that all namespaces are properly declared.
When inserting a node tree under another node that has a different owner document, the ownerDocument property for each inserted node is changed to match the owner document of its new parent.
When moving a node tree to another document, the content of all entity reference nodes contained therein is updated to conform with the new document. So if the new document does not declare an entity that was moved into it, the entity reference will have no children, and the old content is removed. Existing references to nodes under the entity reference are still valid, but the node whose parent perviously was the entity reference now has a null parent.
This operation depends on the value of the nodeType property.
NODE_ATTRIBUTE
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. |
NODE_DOCUMENT
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_DOCUMENT_TYPE, NODE_ELEMENT | Inserts newChild and returns newChild. Note, however, that 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. Note that 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. |
NODE_DOCUMENT_FRAGMENT
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. |
NODE_ELEMENT
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 refChild must be a child node of this node or null. The newChild is inserted before refChild (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.
The parameters can represent nodes in the same document or in different documents. When within the same document, the nodes retain their default attributes and data types. When the 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. The Microsoft implementation 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 type), and default attributes are lost. Note also that cutting and pasting between documents with two different DTDs can result in an invalid document that can 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. Similarly, 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.
When adding a document fragment, the containing element adds all namespaces and prefixes from the document fragment. If this causes a conflict on the containing element, insertBefore returns an error.
If there is a conflict between prefixes on the containing element and the attribute 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 following namespace:
xmlns:myns="URN1"
The error could 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"
Note that namespace conflict errors can 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 on the containing element, insertBefore returns an error.