tags:

views:

57

answers:

1

Anyone know how I can create and save XML using PHP? I need something like this:

<jukebox>
  <track source="" artist="" album="" title="" />
  <track source="" artist="" album="" title="" />
  <track source="" artist="" album="" title="" />
  <track source="" artist="" album="" title="" />
</jukebox>
+4  A: 

Hey Malcolm,

This is probably what you are looking for.


    //Creates XML string and XML document using the DOM 
    $dom = new DomDocument('1.0', 'UTF-8'); 

    //add root
    $root = $dom->appendChild($dom->createElement('Root'));

    //add NodeA element to Root
    $nodeA = $dom->createElement('NodeA');
    $root->appendChild($nodeA);

    // Appending attr1 and attr2 to the NodeA element
    $attr = $dom->createAttribute('attr1');
    $attr->appendChild($dom->createTextNode('some text'));
    $nodeA->appendChild($attr);
/*
** insert more nodes
*/

    $dom->formatOutput = true; // set the formatOutput attribute of domDocument to true

    // save XML as string or file 
    $test1 = $dom->saveXML(); // put string in test1
    $dom->save('test1.xml'); // save as file

For mode info, have a loot at the DOM Documentation.

To do what you want:


    //Creates XML string and XML document using the DOM 
    $dom = new DomDocument('1.0', 'UTF-8'); 

    //add root == jukebox
    $jukebox = $dom->appendChild($dom->createElement('jukebox'));

    for ($i = 0; $i < count($arrayWithTracks); $i++) {

        //add track element to jukebox
        $track = $dom->createElement('track');
        $jukebox->appendChild($track);

        // Appending attributes to track
        $attr = $dom->createAttribute('source');
        $attr->appendChild($dom->createTextNode($arrayWithTracks[$i]['source']));
        $track->appendChild($attr);
        $attr = $dom->createAttribute('artist');
        $attr->appendChild($dom->createTextNode($arrayWithTracks[$i]['artist']));
        $track->appendChild($attr);
        $attr = $dom->createAttribute('album');
        $attr->appendChild($dom->createTextNode($arrayWithTracks[$i]['album']));
        $track->appendChild($attr);
        $attr = $dom->createAttribute('title');
        $attr->appendChild($dom->createTextNode($arrayWithTracks[$i]['title']));
        $track->appendChild($attr);
    }

    $dom->formatOutput = true; // set the formatOutput attribute of domDocument to true

    // save XML as string or file 
    $test1 = $dom->saveXML(); // put string in test1
    $dom->save('test1.xml'); // save as file

Cheers

vfn
Thank's vfn thats exactly what I want and so quickThank's
Malcolm
Hi vfn, thanks again for your response and solution to my question, however if it's ok with you can I pick at your brains a little bit more.The XML file you helped me with would normally be populated with music files "MP3" in a directory that is called using PHP, I can get a list of all the files in a directory using PHP but how would I then add that information to the XML file? the only information I would really need is the source/location of the file's as for the rest I will be using an MP3 metadata class, any help would be greatly appreciated.
Malcolm
What do you mean? I couldn't understand the your question!
vfn
Ok, with the XML file that you helped me with above I would now like to be able to add information from all file's that are in a directory on a web server to it, the main field would be "The source" location of the MP3 file's, so in theory looping through and listing all files in a directory and adding them to the XML file.
Malcolm
Hey Malcolm, It looks like you know the answer to your question. Please, have a look at the edit that I've done to my answer and let me know if it's OK. It's now iterating on an array that will have all the tracks, and for each track a new node will be added to the root of the xml.
vfn
Again I would like to thank you, I now have it sorted, I would be still working on it if it wasn't for you, thank's ;)
Malcolm