PHP Doku:: Adds a new child before a reference node - domnode.insertbefore.html

Verlauf / Chronik / History: (1) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMNode class




(PHP 5)

DOMNode::insertBefore Adds a new child before a reference node


DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

This function inserts a new node right before the reference node. If you plan to do further modifications on the appended child you must use the returned node.



The new node.


The reference node. If not supplied, newnode is appended to the children.


The inserted node.



Raised if this node is readonly or if the previous parent of the node being inserted is readonly.


Raised if this node is of a type that does not allow children of the type of the newnode node, or if the node to append is one of this node's ancestors or this node itself.


Raised if newnode was created from a different document than the one that created this node.


Raised if refnode is not a child of this node.

8 BenutzerBeiträge:
- Beiträge aktualisieren...
felixsigl at gmail dot com
12.05.2009 12:17
I wrote a function for me to handle several insert modes (including insertAfter)


public function myInsertNode($newNode, $refNode, $insertMode=null) {
$insertMode || $insertMode == "inside") {
        } else if(
$insertMode == "before") {
$refNode->parentNode->insertBefore($newNode, $refNode);
        } else if(
$insertMode == "after") {
$refNode->nextSibling) {
$refNode->parentNode->insertBefore($newNode, $refNode->nextSibling);
            } else {

naholyr at yahoo dot fr
8.02.2009 12:52
A very usual needed function is "appendSibling" to insert a node just before another one :

 * @param DOMNode $newnode Node to insert next to $ref
 * @param DOMNode $ref Reference node
 * @requires $ref has a parent node
 * @return DOMNode the real node inserted
function appendSibling(DOMNode $newnode, DOMNode $ref)
  if (
$ref->nextSibling) {
// $ref has an immediate brother : insert newnode before this one
return $ref->parentNode->insertBefore($newnode, $ref->nextSibling);
  } else {
// $ref has no brother next to him : insert newnode as last child of his parent
return $ref->parentNode->appendChild($newnode);
odonnell dot NOSPAM at ideaengineinc dot com
6.05.2008 22:57
Here's a usage example.

If you try this, you'll get an error message.

$refNode->insertBefore($newNode, $refNode);

Try this instead...

$refNode->parentNode->insertBefore($newNode, $refNode);
DrTebi at Yahoo
3.10.2007 14:54
Sorry, my previous posting worked only for the top node. Here the corrected version, which will work for any node:

<?xml version="1.0"?>

// load XML, create XPath object
$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xpath = new DOMXPath($xml);

// get node eva, which we will append to
$eva = $xpath->query('/contacts/person[.="Eva"]')->item(0);

// create node john
$john = $xml->createElement('person', 'John');

// insert john after eva
//   "in eva's parent node (=contacts) insert
//   john before eva's next node"
// this also works if eva would be the last node
$eva->parentNode->insertBefore($john, $eva->nextSibling);

// show result
header('Content-Type: text/plain');

<?xml version="1.0"?>
justin at redwiredesign dot com
7.02.2007 17:20
The previous example is incorrect, and causes a DOM_NOT_FOUND error, as the child nodes are not direct descendants of the root node.

Therefore, the line:

$parent_path = "/root";

needs to change to:

$parent_path = "/root/parent";


$parent_path = "//parent";

for this example to work

25.08.2005 20:34
moving an existing node within the DomDocument:

$dom = new DomDocument();

$parent_path = "/root";
$query = "//child[position() = 2]";
$query2 = "//child[position() = 1]";

$xpath = new DomXPath($dom);

$parent = $xpath->query($parent_path);

$under = $xpath->query($query);

$above = $xpath->query($query2);

$parent->item(0)->insertBefore($under->item(0), $above->item(0));


Should produce:

jg at handcode dot de
18.08.2005 21:18
example to insert <newnode/> between <chid1/> and <child2/>

<?xml version="1.0" encoding="ISO-8859-1" ?>    
    <child nr="1"/>
    <child nr="2"/>

= 'test.xml';
// XPath-Querys
$parent_path = "//parent";
$next_path = "//parent/child[@nr='2']";
// Create a new DOM document
$dom = new DomDocument();
// Find the parent node
$xpath = new DomXPath($dom);
// Find parent node
$parent = $xpath->query($parent_path);
// new node will be inserted before this node
$next = $xpath->query($next_path);
// Create the new element
$element = $dom->createElement('newnode');
// Insert the new element
$parent->item(0)->insertBefore($element, $next->item(0));
Jerry Ellis
3.06.2005 21:45
1st argument) a node to insert
2nd argument) a reference node - this is the node that the new node will be inserted before

The trick to using this method is that the OBJECT on which you actually CALL the insertBefore() method is actually the PARENT node of the reference node! 

$DOMNode_refNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);

$DOMNode_refNode->parentNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);

PHP Powered Diese Seite bei
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",...)