Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMNode->insertBefore()

DOMNode->insertBefore()

Adds a new child before a reference node ()

Examples ( Source code ) » DOMNode->insertBefore()

<?
sample
.xml
<root>
 <
parent>
   <
child><name>Bob</name></child>
   <
child><name>Sue</name></child>
 </
parent>
</
root>
$dom = new DomDocument();
$dom->load("sample.xml");
$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));
$dom->save("sample.xml");
Should produce:
<
root>
 <
parent>
   <
child><name>Sue</name></child>
   <
child><name>Bob</name></child>
 </
parent>
</
root

?>

Code Examples / Notes » dom_domnode_insertbefore

justin

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


drtebi

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>


25-aug-2005 07:34

moving an existing node within the DomDocument:
<root>
 <parent>
   <child><name>Bob</name></child>
   <child><name>Sue</name></child>
 </parent>
</root>
$dom = new DomDocument();
$dom->load("sample.xml");
$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));
$dom->save("sample.xml");
Should produce:
<root>
 <parent>
   <child><name>Sue</name></child>
   <child><name>Bob</name></child>
 </parent>
</root>


drtebi

If you have been wondering why there is no "insertAfter"
function, there is an easy trick to insert an element after another element:
XML file:
---------
<?xml version="1.0"?>
<contacts>
 <person>Adam</person>
 <person>Thomas</person>
</contacts>
PHP Script:
-----------
<?php
// insert person "John" after person "Adam"
$xml = DomDocument::load('contacts.xml');
$adam = $xml->getElementsBytagname('person')->item(0);
$john = $xml->createElement('person', 'John');
$adam->parentNode->insertBefore($john, $adam);
// show result
header('Content-Type: text/plain');
print $xml->saveXML();
?>
Output:
-------
<?xml version="1.0"?>
<contacts>
 <person>John</person><person>Adam</person>
 <person>Thomas</person>
</contacts>
Reference: "XML Matters: Beyond the DOM" <http://www-128.ibm.com/developerworks/xml/library/x-matters41.html>


jg

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

?>


jerry ellis

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


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker