Microsoft XML 2.5 SDK


 

insertBefore Method

[This is preliminary documentation and subject to change.]

Inserts a child node to the left of the specified node or at the end of the list.

Syntax

objXMLDOMNode = oXMLDOMNode.insertBefore(newChild, refChild)

Parameters

newChild

Object. Address of the new node to be inserted.

refChild

Variant. 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.

Returns

Object. On success, returns the child node that was inserted.

Remarks

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 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.

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 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"

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.

Example

The following VBScript example creates a new XMLDOMNode object and inserts it before the second child of the top-level node:

Dim xmlDoc
Dim root
Dim newNode
Dim currNode
Set xmlDoc = CreateObject("microsoft.xmldom")
xmlDoc.async = False
xmlDoc.load("c:\books.xml")
Set root = xmlDoc.documentElement
MsgBox root.xml
Set newNode = xmlDoc.createNode (1, "VIDEOS", "")
Set currNode = root.insertBefore(newNode, root.childNodes.item(1))
MsgBox root.xml

Applies To

XMLDOMAttribute, XMLDOMCDATASection, XMLDOMCharacterData, XMLDOMComment, XMLDOMDocument, XMLDOMDocumentFragment, XMLDOMDocumentType, XMLDOMElement, XMLDOMEntity, XMLDOMEntityReference, XMLDOMNode, XMLDOMNotation, XMLDOMProcessingInstruction, XMLDOMText, XTLRuntime