• 0

[php] write to XML file?


Question

Is it possible to have a PHP script stick the result of a variable in an XML file, in a certain place?

I'm trying to create clips for an mp3 store. My idea is to grab the ProductId from the address bar and generate a clip. for example.

$productId = $_GET['productId'];if (isset($productId) && !empty($productId)) { //code to open XML file.          //code to write to certain part of XML file, see below}

<?xml version="1.0" encoding="UTF-8"?>

<playlist version="0" xmlns = "http://xspf.org/ns/0/">

<trackList>

<track>

<location>/clips/<<WRITE-PRODUCT-ID-HERE>>.mp3</location>

<image></image>

<annotation></annotation>

</track>

</trackList>

</playlist>

[/font]
[font="Courier,"]
&lt;?php
$productId = $_GET['productId'];

if (isset($productId) &amp;&amp; !empty($productId)) {

	$xml = simplexml_load_file("clips.xml");

	$sxe = new SimpleXMLElement($xml-&gt;asXML());
	$clip = $sxe-&gt;addChild("track"); 
	$clip-&gt;addChild("location", $productId); 

	$sxe-&gt;asXML("clips.xml"); 

}
?&gt;

i believe this will add to the XML, but how can i get it to overwrite what's already there?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Just edit in-place fella.

&lt;?php
$strXml = '
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
';

$objXml = new SimpleXMLElement($strXml);
$objXml-&gt;tracklist-&gt;track[0]-&gt;file = '2.mp3';
echo $objXml-&gt;asXML();

/*
 &lt;?xml version="1.0"?&gt;
 &lt;root&gt;
	&lt;tracklist&gt;
 	&lt;track&gt;
 	&lt;file&gt;2.mp3&lt;/file&gt;
 	&lt;/track&gt;
	&lt;/tracklist&gt;
 &lt;/root&gt;
*/
?&gt;

&lt;?php
$strXml = '
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
';

$objXml = new SimpleXMLElement($strXml);
$objXml-&gt;tracklist-&gt;track[1]-&gt;file = '2.mp3';
echo $objXml-&gt;asXML();

/*
&lt;?xml version="1.0"?&gt;
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
	&lt;track&gt;
 	&lt;file&gt;2.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
*/
?&gt;

Link to comment
Share on other sites

  • 0

$productId = $_GET['productId'];
if (isset($productId) &amp;&amp; !empty($productId)) {

Won't really work. Atleast it doesn't do what you are after. The $productId is always set, so you don't need to check it. Instead you should check if the $_GET['productId'] before trying to use it.

$productId = isset($_GET['productId']) ? $_GET['productId'] : '';
if (!empty($productId)) {

Link to comment
Share on other sites

  • 0

Just edit in-place fella.

&lt;?php
$strXml = '
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
';

$objXml = new SimpleXMLElement($strXml);
$objXml-&gt;tracklist-&gt;track[0]-&gt;file = '2.mp3';
echo $objXml-&gt;asXML();

/*
 &lt;?xml version="1.0"?&gt;
 &lt;root&gt;
	&lt;tracklist&gt;
 	&lt;track&gt;
 	&lt;file&gt;2.mp3&lt;/file&gt;
 	&lt;/track&gt;
	&lt;/tracklist&gt;
 &lt;/root&gt;
*/
?&gt;

&lt;?php
$strXml = '
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
';

$objXml = new SimpleXMLElement($strXml);
$objXml-&gt;tracklist-&gt;track[1]-&gt;file = '2.mp3';
echo $objXml-&gt;asXML();

/*
&lt;?xml version="1.0"?&gt;
&lt;root&gt;
 &lt;tracklist&gt;
	&lt;track&gt;
 	&lt;file&gt;1.mp3&lt;/file&gt;
	&lt;/track&gt;
	&lt;track&gt;
 	&lt;file&gt;2.mp3&lt;/file&gt;
	&lt;/track&gt;
 &lt;/tracklist&gt;
&lt;/root&gt;
*/
?&gt;

thanks for that mate, although i'm not really sure whats going on here? is this inside the xml file? how does it parse php

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.