PHP Doku:: Clones a node - domnode.clonenode.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMNode classDOMNode::cloneNode

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMNode class

<<DOMNode::appendChild

DOMNode::getLineNo>>

DOMNode::cloneNode

(PHP 5)

DOMNode::cloneNode Clones a node

Beschreibung

DOMNode DOMNode::cloneNode ([ bool $deep ] )

Creates a copy of the node.

Parameter-Liste

deep

Indicates whether to copy all descendant nodes. This parameter is defaulted to FALSE.

Rückgabewerte

The cloned node.


4 BenutzerBeiträge:
- Beiträge aktualisieren...
cemkalyoncu at gmail dot com
28.04.2009 12:04
If you need some function to clone a node without touching namespaces you can use the following.

<?php
private function cloneNode($node,$doc){
   
$nd=$doc->createElement($node->nodeName);
           
    foreach(
$node->attributes as $value)
       
$nd->setAttribute($value->nodeName,$value->value);
           
    if(!
$node->childNodes)
        return
$nd;
               
    foreach(
$node->childNodes as $child) {
        if(
$child->nodeName=="#text")
           
$nd->appendChild($doc->createTextNode($child->nodeValue));
        else
           
$nd->appendChild(cloneNode($child,$doc));
    }
           
    return
$nd;
}
?>
[montana] at [percepticon] dot [com]
16.02.2009 11:23
<?php

//@oliver thanks for example source...

/*
 cloneNode(false) does not omit
 Attributes of cloned node,
 to achieve this an iteration is required.
 this is probably less efficient
 than merely creating a new
 node from the desired nodeName
 but in some cases could be useful.

use case:

omit subnodes and attributes of
secured portions of an xml document
without altering expected general structure;
*/
//xml to use

$file="<?xml version='1.0'?>
<book type='paperback'>
    <title name='MAP'>Red Nails</title>
    <price>$12.99</price>
    <author>
        <name first='Robert' middle='E' last='Howard'/>
        <birthdate disco='false' nirvana='definitely'>
            9/21/1977
            <month title='september' />
        </birthdate>
    </author>
    <author>
        <name first='Arthur' middle='Mc' last='Kayn'/>
    </author>
</book>"
;

$doc = new domDocument;

$doc->loadXML($file);

$xpath = new domXPath($doc);

$query = "//author/birthdate";
$xpathQuery = $xpath->query($query);

//would be a loop in production code...
$child = $xpathQuery->item(0);

$parent = $child->parentNode;

$doppel = $child->cloneNode(false);

$limit = $doppel->attributes->length;

for (
$a=0;$a<$limit;$a++) {
   
$doppel->removeAttributeNode($doppel->attributes->item(0));
}
//swap for now empty node
$parent->replaceChild( $doppel, $child);

print
$doc->saveXML();

?>
oliver dot christen at camptocamp dot com
19.11.2004 17:48
simple exemple of node cloning

<?xml version="1.0"?>

<book type="paperback">
    <title name='MAP'>Red Nails</title>
    <price>$12.99</price>
    <author>
        <name first="Robert" middle="E" last="Howard"/>
        <birthdate>9/21/1977</birthdate>
    </author>
    <author>
        <name first="Arthur" middle="Mc" last="Kayn"/>
    </author>
</book>

<?php

//filename xml file to use
$file = 'book.xml';

$doc = new domDocument;

if (
file_exists($file)) {
   
$doc->load($file);
} else {
    exit(
'Erreur !.');
}

$xpath = new domXPath($doc);

$query = "//author/*";
$xpathQuery = $xpath->query($query);

$size = $xpathQuery->length;
for (
$i=0; $i<$size; $i++){
   
$node = $xpathQuery->item($i);
    if (
$node->nodeName == 'birthdate' && $node->hasChildNodes() && $node->firstChild->textContent != ''){
       
$clonenode = $node->cloneNode(true);
       
$refnode = $node;
    }
}
for (
$i=0; $i<$size; $i++){
   
$node = $xpathQuery->item($i);
    if (!
$node->isSameNode($refnode)){
       
$newnode = $node->appendChild($clonenode);
    }
}

print
$doc->saveXML();

?>
dtorop932 at hotmail dot com
18.10.2004 19:44
For those converting from PHP 4 DOM XML to PHP 5 DOM extension:

In the old days, DOM XML's clone_node() was the way to copy nodes from one DOM to another, as well as to perform intra-DOM copies.  Nowadays, DOM's cloneNode() is the intra-DOM solution, but see importNode() (http://php.net/dom-domdocument-importnode) to copy nodes from one DOM to another.

So to append root node of $dom2 and all its children into root node of $dom1:

PHP 4 (DOM XML):

<?php
$root1
= $dom1->document_element();
$other_node = $dom2->document_element();
$root1->append_child($other_node->clone_node(true));
?>

PHP 5 (DOM):

<?php
$dom1
->documentElement->appendChild(
 
$dom1->importNode($dom2->documentElement, true));
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)