note 78214 deleted from function.dom-domnode-insertbefore by felipe
| From: | felipe@php.net | Date: | Mon, 24 Dec 2007 03:05:38 +0000 |
| Subject: | note 78214 deleted from function.dom-domnode-insertbefore by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133964@lists.php.net to get a copy of this message | ||
Note Submitter: DrTebi at Yahoo
----
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>