SunshinePHP Developer Conference 2015

DOMNode::insertBefore

(PHP 5)

DOMNode::insertBefore Ajoute un nouveau fils à la fin des enfants

Description

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

Cette fonction insère un nouveau noeud juste avec le noeud de référence. Si vous comptez faire des modifications plus tard sur l'enfant ajouté, vous devez utiliser le noeud retourné.

Liste de paramètres

newnode

Le nouveau noeud.

refnode

Le noeud référencé. Si non spécifié, newnode sera ajouté au fils.

Valeurs de retour

Le noeud inséré.

Erreurs / Exceptions

DOM_NO_MODIFICATION_ALLOWED_ERR

Lancé si le noeud est en lecture seule ou si le parent précédent le noeud à insérer est en lecture seule.

DOM_HIERARCHY_REQUEST_ERR

Lancé si ce noeud est d'un type qui n'autorise pas de fils du type du noeud newnode, ou si le noeud à ajouter est un des ancêtres de ce noeud ou ce noeud lui-même.

DOM_WRONG_DOCUMENT_ERR

Lancé si newnode a été créé depuis un document différent que celui qui a créé ce noeud.

DOM_NOT_FOUND

Lancé si refnode n'est pas un fils de ce noeud.

Voir aussi

add a note add a note

User Contributed Notes 4 notes

up
3
jg at handcode dot de
9 years ago
example to insert <newnode/> between <chid1/> and <child2/>

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

<?php

$xml_src
= 'test.xml';

// XPath-Querys
$parent_path = "//parent";
$next_path = "//parent/child[@nr='2']";

// Create a new DOM document
$dom = new DomDocument();
$dom->load($xml_src);

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

echo
$dom->saveXML();

?>
up
1
justin at redwiredesign dot com
7 years ago
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";

or

$parent_path = "//parent";

for this example to work
up
0
Jerry Ellis
9 years ago
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! 

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

CORRECT:
$DOMNode_refNode->parentNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);
up
-1
DrTebi at Yahoo
7 years ago
Sorry, my previous posting worked only for the top node. Here the corrected version, which will work for any node:

XML
----
<?xml version="1.0"?>
<contacts>
  <person>Adam</person>
  <person>Eva</person>
  <person>Thomas</person>
</contacts>

PHP
---
<?php
// load XML, create XPath object
$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xml->load('contacts.xml');
$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');
print
$xml->saveXML();
?>

Result
------
<?xml version="1.0"?>
<contacts>
  <person>Adam</person>
  <person>Eva</person><person>John</person>
  <person>Thomas</person>
</contacts>
To Top