DOMDocument->importNode()

(no version information, might be only in CVS)

DOMDocument->importNode() -- Import node into current document

说明

class DOMDocument {

DOMNode importNode ( DOMNode importedNode [, bool deep] )

}

This function returns a copy of the node to import and associates it with the current document.

参数

importedNode

The node to import.

deep

If set to TRUE, this method will recursively import the subtree under the importedNode.

返回值

The copied node.

异常

DOMException is thrown if node cannot be imported.


add a note add a note User Contributed Notes
benno at transmog dot nospam dot com dot au
23-Oct-2006 03:04
If deep is false, the node's attributes aren't imported (using PHP 5.0.4). You can use the following if you want to import attributes but not child nodes:

$document->importNode ($node->cloneNode (false), true);
adjwilli
06-Oct-2006 04:08
Editing XML with PHP can be a pain in the Secretary of State Powell, so here's a script to replace an XML node with a user-provided one through the POST. It's usually a good idea to run the $_POST['xml'] through a validation check and clean it for other thing before running this.

Pretty much this script expects a user-provided node called $_POST['xml'] and the XPath of the node in the original document that you want to replace called $_POST['XPath']. It also loads the original XML document from $xml. The function nodeRunner begins with the root node of the document you're editting and the XPath for the root element (these are more to make recursion easy than anything else).

$doc = new DOMDocument();
$doc->loadXML($xml); // $xml expects an XML string
      
function nodeRunner ($node,$xpath) {
   global $doc;
   if ($xpath == $_POST['XPath']) {
          
       $xmlPost = new DOMDocument();
       $xmlPost->loadXML($_POST['xml']);
      
       $newNode = $doc->importNode($xmlPost->firstChild,true);
      
       $node->parentNode->replaceChild($newNode,$node);
   } else {
      
       $page = 1;
       $section = 1;
      
       if ($node->hasChildNodes()) {
           foreach ($node->childNodes as $nodling) {
               $nodeName = $nodling->nodeName;
               if ($nodeName == 'page' || $nodeName == 'section') {
                   nodeRunner ($nodling,$xpath."/".$nodeName."[".$$nodeName."]");
                   $$nodeName++;
               }
           }
       }
   }
}
  
nodeRunner ($doc->documentElement,"/root[1]"); // /root should be explicit name of the root  element of the XPath
  
$doc->saveXML();
stomas
19-Sep-2006 09:14
I think this should do the same:

<?
 
// Import $b into $a's document
 
function myimport($a, $b)
 {
   if (
$a->ownerDocument->isSameNode($b->ownerDocument))
   {
   return
$b->cloneNode(TRUE);
   }
   else
   {
   return
$a->ownerDocument->importNode($b, TRUE);
   }
 }
 
?>
Colin
13-Sep-2006 12:11
As of PHP 5.1.6 with libxml 2.6.26 and DOM/XML API version 20031129, importNode() does nothing if attempting to import from the same document.  Meaning, if you do an $ret = importNode and then appendChild($ret) or insertBefore($ret, ...) then you will end up *moving* the node instead of ending up with a copy.

If you expect importNode to give you a copy of the source (in this case, a deep copy) then you must account for them being from the same document.  This function addresses this:

<?
// Import $b into $a's document
function myimport($a, $b)
{
  if (
$a->ownerDocument->isSameNode($b->ownerDocument))
  {
  
$temp = new DOMDocument();
  
$ret = $temp->importNode($b, TRUE);
   return
$a->ownerDocument->importNode($ret, TRUE);
  }
  else
  {
   return
$a->ownerDocument->importNode($b, TRUE);
  }
}
?>

(Function was freshly coded for this note but I based it off another, working function of mine.)

This function checks if the documents are the same and uses a new document (guaranteed to be different this way) to force a copy into $temp and then force a copy back into $a->ownerDocument.

Obviously, no error checking has been done.
andy dot clark at dial dot pipex dot com
16-Aug-2006 09:10
Please ignore the previous comment, it's incorrect. Fitopaldi's example of 3rd Aug 2005 is all you need to copy a node and it's sub nodes. This is what the "deep" flag is doing.
andy dot clark at dial dot pipex dot com
15-Aug-2006 11:20
One useful use of importNode is to copy one node onto another.

function CopyXMLNode($SourceNode,$DestNode)
 {
  if ($SourceNode->nodeName != '#text')
   {
     //Copy this node
   $node = $DestNode->ownerDocument->importNode($SourceNode, true);
   $node = $DestNode->appendChild($node);
   //Now copy the child nodes
   foreach ($SourceNode->childNodes AS $item)
     {
     $this->CopyXMLNode($item,$node);
     }
   }
  }
TJ <php at tjworld dot net>
04-Nov-2005 05:15
Beware if you're working with classes you've extended from DOMNode, DOMElement, etc.

Calling importNode() with an object of your derived class will result in the copy being an instance of the DOM super class.

You will *not* get a copy of the derived class or any of its properties or methods.

For example:

<?
class extDOMElement extends DOMElement {
 function
__construct($name, $value='', $namespaceURI=null) {
 
parent::__construct($name, $value, $namespaceURI);   
 }
}

class
extDOMDocument extends DOMDocument {
 
// over-ride DOMDocument->createElement()
 
public function createElement($name, $value) {
 
// intention is to set extDOMElement->ownerDocument property
 
$orphan = new extDOMElement($name, $value);
 
$adopt = $this->importNode($orphan, true);
 
/* at this point $adopt is expected to be an instance of
   * extDOMElement with ownerDocument property set
   */
  
return $adopt; // but $adopt is only a DOMElement
 
}
}

$doc = new extDOMDocument();
$el = $doc->createElement('tagName');
echo
'Element instanceof '.get_class($el);
?>

Will display "Element instanceof DOMElement" rather than the expected "extDOMElement"
Fitopaldi
04-Aug-2005 02:29
importNode returns a copy of the node to import and associates it with the current document, but not import the node to the current DOMDocument. Use appendChild for import the copy of the node to current DOMDocument.

<?
 $domNode
= $dom->importNode($aDomNode, true);
 
$currentDomDocument->appendChild($domNode);
?>