-1

I would like to add some data to a XML file using PHP. I'm quite new and so do not know much of the terminology and so will try to explain it as much as possible. Thanks Joe

XML FILE:

<?xml version="1.0" encoding="UTF-8"?>
<music>
    <song>
        <title>Example Song</title>
        <album>A Album</album>
        <artist>A Artist</artist>
        <length>3.41</length>
    </song>
//The New Song I Would Like To Add For Example
    <song>
        <title>Another Example Song</title>
        <album>Another A Album</album>
        <artist>Another A Artist</artist>
        <length>Another 3.41</length>
    </song>
//The End Of The New Song I Would Like To Add

</music>

3 Answers3

1

This should do the trick:

$xml_load = simplexml_load_file($Your_xml);
$song = $xml_load->addChild("song");
$song->addChild("title", "song title");
$song->addChild("album", "song album");
$song->addChild("artist", "song artist");
$song->addChild("length", "03:01");

echo $xml_load->saveXML()
NorthernLights
  • 370
  • 3
  • 16
0

Try using PHP's SimpleXML

The SimpleXML usage page gives a good tutorial of how to use it.

Maltronic
  • 1,782
  • 10
  • 21
0

I haven't tested that but I think that it would be work

With SimpleXML

// Read file    
$music = simplexml_load_file('music.xml');

$character = $music->addChild('song');
$character->addChild('title', 'Another Example Song');
$character->addChild('album', 'Another A Album');
$character->addChild('artist', 'Another A Artist');
$character->addChild('length', 'Another 3.41');

And then force download the new file

Community
  • 1
  • 1
Seyn
  • 42
  • 1
  • 4