Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is probably what you are looking for.</p> <pre><code> //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 </code></pre> <p>For more information, have a look at the <a href="http://www.php.net/manual/en/book.dom.php" rel="noreferrer">DOM Documentation</a>.</p> <p>To do what you want:</p> <pre><code> //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 &lt; 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 </code></pre> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload