INSERTING INFORMATION INTO XML FILE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

INSERTING INFORMATION INTO XML FILE

Post 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?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: INSERTING INFORMATION INTO XML FILE

Post 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());
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: INSERTING INFORMATION INTO XML FILE

Post 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
Post Reply