DOM append to the top
am 20.11.2009 11:23:41 von Matthew Croud
Yo!
I have a working form that adds user input to an XML file, it adds the
new "item" element to the bottom of the list of previously created
"item" elements.
I would now like the new elements to be added to the top of the list.
So far i've tried using: insertBefore() but i'm still getting elements
added to the bottom.
The XML looks like:
- ...
- ...
- ...
<----- new items are added here
This is what my code looks like:
$y = $dom -> getElementsByTagName("item");
$dom -> documentElement -> insertBefore($Xitem,$y);
If trying to pass the top most "item" node to insertbefore() but
without any luck. Any ideas ?
Cheers
Mongoose
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: DOM append to the top
am 20.11.2009 16:53:45 von Nathan Rixham
Matthew Croud wrote:
> The XML looks like:
>
>
> - ...
> - ...
> - ...
> <----- new items are added here
>
>
> This is what my code looks like:
>
> $y = $dom -> getElementsByTagName("item");
> $dom -> documentElement -> insertBefore($Xitem,$y);
>
> If trying to pass the top most "item" node to insertbefore() but without
> any luck. Any ideas ?
been very verbose on purpose so you get the idea ->
$newItem = ''; // your new node to insert
$items = $dom->getElementsByTagName("item");
$firstItem = $items->item(0);
$clothes = $firstItem->parentNode;
$newItem = $clothes->insertBefore( $newItem , $firstItem );
short version is..
$y = $dom->getElementsByTagName("item")->item(0);
$Xitem = $y->parentNode->insertBefore( $Xitem, $y );
please note: haven't checked this but should all work.
problem with your code was you were supplying $y which was a
DOMNodeList; and as the docs say: "If not supplied, newnode is appended
to the children." which yuo can translate as it'll be appended if the
second param of insertBefore is incorrect i guess.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php