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
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);
?>