Page 1 of 1

Changing values of SimpleXML children

Posted: Thu Feb 05, 2009 5:53 pm
by Still Learning
This is the XML file I am referencing.
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="0" xmlns = "http://xspf.org/ns/0/">

<title>Xeroform on Want-Signed.com</title>
<trackList>

<track>
<location>http://www.want-signed.com/artists/xeroform/Drown You.mp3</location>
<image>http://www.want-signed.com/artists/xero ... jpg</image>
<annotation>Drown You</annotation>
</track>

<track>
<location>http://www.want-signed.com/artists/xero ... </location>
<image>http://www.want-signed.com/artists/xero ... jpg</image>
<annotation>Rebate</annotation>
</track>

<track>
<location>http://www.want-signed.com/artists/xeroform/Mary Cover.mp3</location>
<image>http://www.want-signed.com/artists/xero ... jpg</image>
<annotation>Mary Cover</annotation>
</track>

</trackList>
</playlist>
I'm trying to change the value of one of the attributes, in this example just the value for "annotation". (The name of the song)

Code: Select all

 
<?php
$artist= $_GET["artist"];
$songnumber= $_GET["song_number"];
$newsongname= $_POST["newname"];
$root = $_SERVER['DOCUMENT_ROOT'];
$file = $root . "/whereallmyxspffilesare/" . $artist . ".xspf";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
$xml->trackList[0]->track[$songnumber]->annotation = $newsongname;
$fh = fopen($file, "w+");
fwrite($fh, $xml->asXML() );
fclose($fh);
$songnumber++;
echo "Song number " . $songnumber . " name changed to " . $newsongname;
?>
 
I've tested that all the $_GET's and $_POST's are successfully loaded, but I can't seem to change the value for annotation. There's no syntax errors, and it echo's the success line #13 as if everything works(but it doesn't). Can someone please help?

The problem is on line 9 I know that much.

*I've read about a thousand tutorials and I can't figure out what's wrong*

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 2:18 am
by susrisha
you are trying to open the same file twice.

Code: Select all

 
//comment from here
$fh = fopen($file, "w+");
fwrite($fh, $xml->asXML() );
fclose($fh);
//to here
//replace it with
$xml->asXML($file);
 

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 11:37 am
by Still Learning
Hmmmmm. Still not working. Am I not referencing the annnotation correctly? I'm still having the same problem.

Can ya take another look at line 9?


**I actually deleted the code you said to comment, so that's not an issue anymore.**

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 12:10 pm
by Still Learning
After loading the file I added this code:

Code: Select all

 
$xml->trackList[0]->track[$songnumber]->annotation= $newsongname;
$xml->asXML($file);
$songnumber++;
echo "Song number " . $songnumber . " name changed to " . $newsongname;
echo "<BR>" . $xml->trackList[0]->track[$songnumber - 1]->annotation;
 
this is the output I get:
Song number 3 name changed to Sublime Cover
Mary Cover
It's strange because I am referencing the annotation the same in both instances, yet it's not making the changes to the file as evidenced by the old name showing up underneath the new name.

:banghead:

Ideas?

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 8:57 pm
by susrisha

Code: Select all

 
$xml->trackList[0]->track[$songnumber]->annotation= $newsongname; //this is where you have gone wrong
$xml->asXML($file);
$songnumber++;
echo "Song number " . $songnumber . " name changed to " . $newsongname;
echo "<BR>" . $xml->trackList[0]->track[$songnumber - 1]->annotation; //this is where you have gone wrong
 
I have run the same program in my system using the exact inputs that you have given. I am posting the out put file. The problem is with chosing the song number

Code: Select all

 
$xml->trackList[0]->track[$songnumber]->annotation= $newsongname;//incorrect
$xml->trackList[0]->track[$songnumber-1]->annotation= $newsongname;//correct
 
Here is the output xml file i got using your code:
Input

Code: Select all

 
$xml->trackList[0]->track[$songnumber]->annotation= $newsongname;
 
Output xml file

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="0">
 
<title>Xeroform on Want-Signed.com</title>
<trackList>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Drown You.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/drowning.jpg</image>
<annotation>Drown You</annotation>
</track>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Rebate.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/defaultpic.jpg</image>
<annotation>Rebate</annotation>
</track>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Mary Cover.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/Sublime-RobbinTheHood.jpg</image>
<annotation>Mary Cover</annotation>
</track>
 
<track><annotation>Sublime Cover</annotation></track></trackList>
</playlist>
 
Code replaced with input

Code: Select all

 
$xml->trackList[0]->track[$songnumber-1]->annotation = $newsongname;
 
Output

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="0">
 
<title>Xeroform on Want-Signed.com</title>
<trackList>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Drown You.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/drowning.jpg</image>
<annotation>Drown You</annotation>
</track>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Rebate.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/defaultpic.jpg</image>
<annotation>Rebate</annotation>
</track>
 
<track>
<location>http://www.want-signed.com/artists/xeroform/Mary Cover.mp3</location>
<image>http://www.want-signed.com/artists/xeroform/Sublime-RobbinTheHood.jpg</image>
<annotation>Sublime Cover</annotation>
</track></trackList>
</playlist>
 
The inputs that i manually enter were :

$songnumber= 3;
$newsongname='Sublime Cover';

Hope that would suffice your requirement

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 9:35 pm
by Still Learning
Thanks for your help. I'm not sure why adding the -1 works, because I was sending song numbers starting with zero from the other form. (So song 3 was actually song 2 in the array) When I tried the code with -1 it updated the previous song, which was a good start.


Finally, I just added 1 to the $songnumber at the $_GET["song_number"] then subtracted it later where you suggested and now it works.


Don't understand exactly why i had to do that but now it works so thank you so much! I really appreciate your time!

Also I noticed I didn't have php5 installed earlier today (I had 4.4.9) and submitted a ticket to my server after installing it, so maybe they restarted apache for me and that's why it works now.

Who knows? I'm just glad i'm not pulling my hair out anymore.

:)

Re: Changing values of SimpleXML children

Posted: Fri Feb 06, 2009 9:48 pm
by susrisha
glad that i was of some help.. now edit the thread topic as [solved] . :)