php5 and xml

This is a discussion on php5 and xml within the PHP Language forums, part of the PHP Programming Forums category; I am trying to find a way to add a child node at the top of the node tree. My ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-04-2005
Andrew M.
 
Posts: n/a
Default php5 and xml

I am trying to find a way to add a child node at the top of the node
tree. My xml document has root <songs> and child elements <song>. I need
to be able to insert <song> elements at the top not at the bottom like
appendChild.

Thanks for any help.
Reply With Quote
  #2 (permalink)  
Old 01-04-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: php5 and xml

Andrew M. wrote:
> I am trying to find a way to add a child node at the top of the node
> tree. My xml document has root <songs> and child elements <song>. I
> need to be able to insert <song> elements at the top not at the
> bottom like appendChild.
>


Use insertBefore, examples:

<?php

$xml = new DomDocument();

// Create and append the root node
$root = $xml->createElement("root");
$xml->appendChild($root);

// Create and add the first child
$child = $xml->createElement("song");
$child->setAttribute("number", 1);
$firstChild = $root->appendChild($child);

// Create and add another child
$child = $xml->createElement("song");
$child->setAttribute("number", 2);
$root->insertBefore($child, $firstChild);

?>

*Or*

<?php

$xml = new DomDocument();

// Create and append the root node
$root = $xml->createElement("root");
$xml->appendChild($root);

// Create and add the first child
$child = $xml->createElement("song");
$child->setAttribute("number", 1);
$root->appendChild($child);

// Create and add another child
$child = $xml->createElement("song");
$child->setAttribute("number", 2);

// Find the first song child element
$firstChild = $root->getElementsByTagName("song")->item(1);
$root->insertBefore($child, $firstChild);

?>


JW



Reply With Quote
  #3 (permalink)  
Old 01-04-2005
Janwillem Borleffs
 
Posts: n/a
Default Re: php5 and xml

Janwillem Borleffs wrote:
> // Find the first song child element
> $firstChild = $root->getElementsByTagName("song")->item(1);
>


This example is incorrect, you should get the first item:

$firstChild = $root->getElementsByTagName("song")->item(0);


JW



Reply With Quote
  #4 (permalink)  
Old 01-05-2005
Andrew M.
 
Posts: n/a
Default Re: php5 and xml

Thank you very much Janwillem. That did the trick . I had a feeling it
was insertBefore but I was having a lot of trouble tracking down
documentation about how to use it.

Janwillem Borleffs wrote:

> Janwillem Borleffs wrote:
>
>>// Find the first song child element
>>$firstChild = $root->getElementsByTagName("song")->item(1);
>>

>
>
> This example is incorrect, you should get the first item:
>
> $firstChild = $root->getElementsByTagName("song")->item(0);
>
>
> JW
>
>
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:58 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0