note 78214 added to function.dom-domnode-insertbefore
| From: | DrTebiatYahoo at osu1 dot php dot net | Date: | Wed, 03 Oct 2007 10:20:14 +0000 |
| Subject: | note 78214 added to function.dom-domnode-insertbefore | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131030@lists.php.net to get a copy of this message | ||
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>
----
Server IP: 209.41.74.194
Probable Submitter: 69.12.130.153
----
Manual Page -- http://www.php.net/manual/en/function.dom-domnode-insertbefore.php
Edit -- https://master.php.net/note/edit/78214
Del: integrated -- https://master.php.net/note/delete/78214/integrated
Del: useless -- https://master.php.net/note/delete/78214/useless
Del: bad code -- https://master.php.net/note/delete/78214/bad+code
Del: spam -- https://master.php.net/note/delete/78214/spam
Del: non-english -- https://master.php.net/note/delete/78214/non-english
Del: in docs -- https://master.php.net/note/delete/78214/in+docs
Del: other reasons-- https://master.php.net/note/delete/78214
Reject -- https://master.php.net/note/reject/78214
Search -- https://master.php.net/manage/user-notes.php