Page 1 of 1

INSERTING INFORMATION INTO XML FILE

Posted: Sat Mar 19, 2011 10:55 pm
by fugix
Hey guys,
I am trying to insert data into an xml file each time an if() function is triggered. Here is the xml file

<?xml version="1.0"?>
<playlist version="1">

<trackList>

<track>
<location>members/1/test1.mp3</location>
<artist>test</artist>
<title>test</title>

</track>
<track>
<location>members/1/tu_presencia</location>
<artist>Alex Zurdo</artist>
<title>Tu Presencia</title>

</track>
<track>
<location>members/1/03 - agradecerte</location>
<artist>Alex Zurdo</artist>
<title>Agradecerte</title>

</track>
</trackList>
</playlist>

and here is the php file to insert new information.

<?php

$mid = mysql_insert_id();
$mq = mysql_query("SELECT * FROM updates WHERE userid='".$userid."' AND id='".$mid."'");
$mrow = mysql_fetch_assoc($mq);
$mpath = $mrow['musicpath'];

$filename = "/var/www/members/1/playlist.xml";
$xml = simplexml_load_file($filename);

$sxe = new SimpleXMLElement($xml->asXML());

$track = $sxe->addChild("track");
$track->addChild("location", "test");
$track->addChild("title", "test");
echo "<br />";

$sxe->asXML("/var/www/members/1/playlist.xml");

?>

now I have the insertion working fine, however, it inserts the information between the closing </tracklist> and </playlist> tags. Which renders the information useless. I need to get the dynamic information to be inserted before the </tracklist> tag and the </playlist> in order to use the information. Any suggestions?

Re: INSERTING INFORMATION INTO XML FILE

Posted: Sun Mar 20, 2011 2:19 am
by Zyxist
Then don't add the new node directly into the <playlist>, but go a level deeper and find <tracklist>. Here's the code where you pick up the wrong node:

Code: Select all

$xml = simplexml_load_file($filename);
$sxe = new SimpleXMLElement($xml->asXML());

Re: INSERTING INFORMATION INTO XML FILE

Posted: Sun Mar 20, 2011 9:38 pm
by fugix
if my xml consists of just <playlist> <trackList> </trackList> </playlist>...how would i go about addind info in between the <trackList> tag and not the <playlist> tag? essentially ignoring the playlist tag.? thank you for your help