Adding data to xml using php(simplexml)

I got this form that exports data and attached a piece of php code that enters the data into a xml file.

So far I managed to get the data wanted into the XML file but only at wrong spot.
Lets start with sample of XML file.

<?xml version="1.0" encoding="UTF-8"?> <playlist xmlns="http://xspf.org/ns/0/" version="1"> <trackList> <track> <title>Episode 1</title> <location>./Movies/krex/krex01.mp4</location> <image>./Movies/krex/krex01.jpg</image> </track> <track> <title>Episode 2</title> <location>./Movies/krex/krex02.mp4</location> <image>./Movies/krex/krex02.jpg</image> </track> <track> <title>Episode 3</title> <location>./Movies/krex/krex03.mp4</location> <image>./Movies/krex/krex03.jpg</image> </track> <track> <title>Episode 4</title> <location>./Movies/krex/krex04.mp4</location> <image>./Movies/krex/krex04.jpg</image> </track> </trackList>

What happens when I use form to add next entry:

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



Episode 1
./Movies/krex/krex01.mp4
./Movies/krex/krex01.jpg


Episode 2
./Movies/krex/krex02.mp4
./Movies/krex/krex02.jpg


Episode 3
./Movies/krex/krex03.mp4
./Movies/krex/krex03.jpg


Episode 4
./Movies/krex/krex04.mp4
./Movies/krex/krex04.jpg

Episode 05 ./Movies/krex/krex05.mp4 ./Movies/krex/krex05.jpg [/code]

As you can see the following entry is entered at the bottom.
What I want is it to be added after the last track in between and

As for PHP code handling the code:
[php]<?php
if (isset($_POST[‘submit’])) {
$fname = ($_POST[‘playlist’]);
$title = ($_POST[‘title’]);
$location = ($_POST[‘location’]);
$name = ($_POST[‘name’]);

echo $title;
echo “$fname”;
$xml = simplexml_load_file(“xml/$fname.xml”); //This line will load the XML file.

$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$track = $sxe->addChild(“track”);
$track->addChild(“title”, “$title”);
$track->addChild(“location”, “$location$name.mp4”);
$track->addChild(“image”, “$location$name.jpg”);

$dom = new DOMDocument(‘1.0’);
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
//Save XML to file - remove this and following line if save not desired
$dom->save(“xml/$fname.xml”);
}
?>[/php]

Php code might be bit messy but gets (most) of the job done.

Feel free to give general advice as to improve any codes in here as well though, I am always willing to learn, but most important thing to me at this moment is getting it located in right place.

So what your saying is how do I append to the same xml sheet ?

so you had the example and tried to post for it to be added but it did not go in to the right place I hope this is what you are trying to say.

I added trackList-> to the line

So change this
[php]$track = $sxe->addChild(“track”);[/php]
To this
[php]$track = $sxe->trackList->addChild(“track”);[/php]

the result
[php]<?xml version="1.0" encoding="UTF-8"?>



Episode 1
./Movies/krex/krex01.mp4
./Movies/krex/krex01.jpg


Episode 2
./Movies/krex/krex02.mp4
./Movies/krex/krex02.jpg


Episode 3
./Movies/krex/krex03.mp4
./Movies/krex/krex03.jpg


Episode 4
./Movies/krex/krex04.mp4
./Movies/krex/krex04.jpg


mytitle
D:\name one.mp4
D:\name one.jpg


[/php]

That sounds about right, I am at my work right now so I can’t really test it myself.
Sounds like you did test it yourself and result looks exactly as I want it.

I would test this as soon as I get home.
Thank you for the help, I now also understand how it works, thats maybe even more important then just having it work.

Sponsor our Newsletter | Privacy Policy | Terms of Service