InsertNode method: XmlNode class

Syntax

InsertNode(&NewNode, {&RefNode | Position})

Description

Use the InsertNode method to insert a new node. The node is inserted either before the node specified by &RefNode, or at the location specified by Position.

&NewNode refers to an already instantiated XmlNode object. You must create the node before you can insert it.

&RefNode refers to an already instantiated XmlNode object.

Parameters

Parameter Description

&NewNode

Specify the name of an already instantiated XmlNode that you want to insert.

&RefNode | Position

Specify either the name of an existing node or the location where you want the node to be inserted.

Returns

A reference to the newly inserted node.

Example

The following PeopleCode program inserts a node using a position:

Local XmlDoc &inXMLDoc, &firstDoc;
Local XmlNode &childNode;

&firstDoc = CreateXmlDoc("<?xml version='1.0'?><myroot><third><child/></third><⇒
/myroot>");
&inXMLDoc = CreateXmlDoc("<?xml version='1.0'?><root><first/><second/></root>");
&childNode = &firstDoc.DocumentElement.FindNode("third");

&inXMLDoc.DocumentElement.InsertNode(&childNode, 2);

The following PeopleCode program inserts a node using a reference node:

Local XmlDoc &inXMLDoc, &firstDoc;
Local XmlNode &childNode, &secondNode;

&firstDoc = CreateXmlDoc("<?xml version='1.0'?><myroot><third><child/></third><⇒
/myroot>");
&inXMLDoc = CreateXmlDoc("<?xml version='1.0'?><root><first/><second/></root>");
&childNode = &firstDoc.DocumentElement.FindNode("third");
&secondNode = &inXMLDoc.DocumentElement.FindNode("second");

&inXMLDoc.DocumentElement.InsertNode(&childNode, &secondNode);

The following is the XML document before the insert:

<?xml version="1.0"?>
<root>
  <first/>
  <second/>
</root>

The following is the XML document after the insert:

<?xml version="1.0"?>
<root>
  <first/>
  <third>
    <child/>
  </third>
  <second/>
</root>