JavaScript method: XML.importNode()

This method copies an XML node from one XML document to another. It requires two existing XML objects with document elements: one is the source XML object and the other is the target XML document. This method does not update either the source or the target XML object. Instead, you must use the appendNode method to add the imported node into the target XML document.

Syntax

XML.importNode( name );

Arguments

The following arguments are valid for this method:

Argument Data type Description
name String This argument specifies the XML node name you want to copy from the source XML object. The string argument must contain characters valid for XML (for example, the string cannot include the characters < or > except as XML entities such as &lt; and &gt;).

Return values

An XML object or null.

The method returns an XML object containing the copied node or returns null if the method cannot import a copy of the node.

Example

This example imports an XML node from one XML object to another.

This example requires the following sample data:

  • A local XML file (for example, C:\test.xml which contains <document><parent1><child1 attribute1="a" /><child2 /></parent1><parent2><child3 /><child4 /></parent2></document>)
  • An XML object (for example, an object containing <document> </document>)
var XMLFile = new XML();
var XMLObject = new XML();
var TargetNode;

function findTargetElement( node, targetElem )
{
 var topNodeName = node.getNodeName();
 while (node != null && node.getNodeName() != targetElem )
 {
  var childNode = node.getFirstChildElement();
  if (childNode == null)
  {
   childNode = node.getNextSiblingElement();
   while (childNode == null)
   {
    node = node.getParentNode();
    if ( node == null || topNodeName == node.getNodeName() )
    {
     return null;
    }
    childNode = node.getNextSiblingElement();
   }
   node = childNode;
  }
  else
  {
   node = childNode;
  }
 }
 return node;
}

/* Create a function to print the target element and search results */
function printFindResults( target, searchResult )
{
 if ( searchResult != null )
 {
  var elementName = searchResult.getNodeName();
  print( "Found element " + elementName + " in: \n" + searchResult + "\n" );
  return searchResult
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

function importNewNode( xmlSource, xmlTarget, node )
{
 print( "The XML source object is:\n" + xmlSource );
 print( "The target element we are looking for is: " + node );
 var FoundNode = findTargetElement( xmlSource, node );
 printFindResults( node, FoundNode );
 print( "The XML target object is:\n" + xmlTarget );
 print( "Importing " + FoundNode + " into XML target object..." );
 var NewNode = xmlTarget.importNode( FoundNode );
 print( "The XML target object remains unchanged: " +  xmlTarget );
 print( "Appending " + FoundNode + " into XML target object...");
 var appendedNode = xmlTarget.appendNode( NewNode );
 print( "The new XML target object is: " +  xmlTarget );
}

XMLFile = XMLFile.setContent( "C:\\test.xml", true );
XMLObject = XMLObject.setContent( "<document> </document>" );
TargetNode = "parent1";
importNewNode( XMLFile, XMLObject, TargetNode );