PHP: DOMDocument->importNode() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument->load() DOMDocument->getElementsByTagNameNS()

Last updated: Fri, 26 Jan 2007

view this page in

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

## Errors/Exceptions

DOMException is thrown if node cannot be imported.

add a note User Contributed Notes
DOMDocument->importNode()
adjwilli
05-Oct-2006 01: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 06: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
12-Sep-2006 09: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
15-Aug-2006 08: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);
}
}
}`
Fitopaldi
03-Aug-2005 11: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);
?>`
add a note
DOMDocument->load() DOMDocument->getElementsByTagNameNS()

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST