Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save XML using PHP

Tags:

php

xml

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>
like image 877
Malcolm Avatar asked Aug 24 '10 00:08

Malcolm


People also ask

How do I save a XML file?

Click File > Save As, and select the location where you want to save the file. , point to the arrow next to Save As, and then click Other Formats. In the File name box, type a name for the XML data file. In the Save as type list, click XML Data, and click Save.

How can I download XML file in PHP?

php header('Content-type: text/xml'); header('Content-Disposition: attachment; filename="text. xml"'); echo $xml_contents; exit(); Otherwise you'll get the whole buffered page, not only the XML output. Show activity on this post.

Can I use PHP in XML?

If you run the XML file as a PHP script, then yes.


1 Answers

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 more information, have a look 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

like image 72
vfn Avatar answered Sep 20 '22 05:09

vfn