PHP XML INSERTION

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

PHP XML INSERTION

Post by fugix »

hey guys,
Im trying to insert data into my xml file using php. I can get every working except i need to bypass the root tag. my xml file looks like this...

<playlist>
<trackList>

</trackList>
</playlist>

What i need to do is insert information between the <trackList> tags but the default is to insert the information before the <trackList> tag...right after the <playlist> tag... any thoughts on this? thank you
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP XML INSERTION

Post by Weiry »

Are you generating the file dynamically as each page is loaded, or are you trying to edit a physical file?
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: PHP XML INSERTION

Post by fugix »

im trying to insert into a file that is already made..
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP XML INSERTION

Post by Jonah Bron »

Please show your code.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: PHP XML INSERTION

Post by fugix »

well my xml file is above.. and heres my php..

<?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'];
$mname = $mrow['musicname'];

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

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

$playlist = $sxe->addChild("trackList")->addChild("track");
$playlist->addChild("location", $mpath);
$playlist->addChild("title", $mname);
echo "<br />";

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

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP XML INSERTION

Post by Jonah Bron »

Well, first, this is redundant.

Code: Select all

$xml = simplexml_load_file($filename);

$sxe = new SimpleXMLElement($xml->asXML());
Just needs to be this:

Code: Select all

$sxe = simplexml_load_file($filename);
And, I think you want this:

Code: Select all

$playlist = $sxe->trackList->addChild("track");
Instead of this:

Code: Select all

$playlist = $sxe->addChild("trackList")->addChild("track");
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: PHP XML INSERTION

Post by fugix »

that worked well. Thank you
Post Reply