473,321 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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.
Jul 17 '05 #1
3 2063
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

Jul 17 '05 #2
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

Jul 17 '05 #3
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

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christoph Nothdurfter | last post by:
Hallo! I was wondering if my PHP4-Scripts will run under PHP5 (Haeven't tried the beta yet). Does anybody know? Thank you, -Christoph
8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
5
by: Tim Tyler | last post by:
I'm sure this is a FAQ - but I could not find a coherent statement of the answer: Some of my clients want PHP4. Other ones want PHP5. Can I run both PHP4 and PHP5 under the same instance of...
11
by: neur0maniak | last post by:
Hi, I've been eager to try out PHP5, so I've dumped it on my little dev machine. It's running WinXP with IIS5. I've put the php-cgi.exe in the "mappings" page as I'm used to doing with PHP4....
4
by: badbetty | last post by:
Dear Googlers I have installed PHP5 to run on WinXP against Apache 2. It works! ie. I have tested a few simple scripts and a basic xml document parse. I now want to try the XSL extension so I...
5
by: Aziz | last post by:
Hi, I've recently contacted technical service of a web hosting company and asked them wheter or not they're gonna upgrade to PHP5 and MySQL5. Here's a quote from their response which confused me...
4
by: Chuck Anderson | last post by:
I am trying to install Php5 on my WindowsXP machine (at home, not on the 'net). I downloaded the Php Windows binary at php.net, unzipped it to c:/Php5, changed my Apache config file to use Php5...
19
by: McKirahan | last post by:
I am working in two environments neither configuration of which I can change; one's my Web host the other a client. My Web host requires the use of the ".php5" extension to use PHP v5.1.4; where...
3
by: xhe | last post by:
I have just upgraded my php version form php4 to php5. and I met this problem, and don't know if you know the solution. My site was written in PHP4, and most parts can be running smoothly in PHP5,...
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.