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
PHP XML INSERTION
Moderator: General Moderators
Re: PHP XML INSERTION
Are you generating the file dynamically as each page is loaded, or are you trying to edit a physical file?
Re: PHP XML INSERTION
im trying to insert into a file that is already made..
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP XML INSERTION
Please show your code.
Re: PHP XML INSERTION
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");
?>
<?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");
?>
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP XML INSERTION
Well, first, this is redundant.
Just needs to be this:
And, I think you want this:
Instead of this:
Code: Select all
$xml = simplexml_load_file($filename);
$sxe = new SimpleXMLElement($xml->asXML());Code: Select all
$sxe = simplexml_load_file($filename);Code: Select all
$playlist = $sxe->trackList->addChild("track");Code: Select all
$playlist = $sxe->addChild("trackList")->addChild("track");Re: PHP XML INSERTION
that worked well. Thank you