0

I used the linked post to try out generating some XML in PHP, but in the browser when it echos the XML, it's just a string, with no structure. What am I doing wrong? https://stackoverflow.com/a/487629/2702121

Here's an image of the output:browser output

Here's the code in my PHP file:

/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('0.9', 'UTF-8');

/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

$currentTrack = $domtree->createElement("track");
$currentTrack = $xmlRoot->appendChild($currentTrack);

/* you should enclose the following two lines in a cicle */
$currentTrack->appendChild($domtree->createElement('path','song1.mp3'));
$currentTrack->appendChild($domtree->createElement('track_title','title of song1.mp3'));

$currentTrack->appendChild($domtree->createElement('path','song2.mp3'));
$currentTrack->appendChild($domtree->createElement('track_title','title of song2.mp3'));

/* get the xml printed */
echo $domtree->saveXML();
Community
  • 1
  • 1
Jen Born
  • 741
  • 1
  • 6
  • 20
  • Works fine - https://eval.in/106736 - check your source code? – scrowler Feb 28 '14 at 02:42
  • You have to pass the header to the browser so that the content-type is clear: `header('Content-type: application/xml; charset=utf-8');`. – hakre Mar 04 '14 at 16:18

1 Answers1

0

It looks like you are using Firefox to view the XML, and it has no stylesheet to sytle the unknown elements. Therefore it is just outputing the text nodes of all of the elements in document order.

If you view the source you will likely see that it is, in fact, well-formed XML.

You can confirm this by right-clicking in the page, and selecting "View Source" from the context menu.

  • Brilliant! Thanks! Is there a standard stylesheet I can apply to view it in all of it's glory? – Jen Born Feb 28 '14 at 02:47
  • @JenBorn You'd need to write a custom XSLT stylesheet to display it, but its probably more trouble than its worth. I presume that your XML is being consumed by another program? –  Feb 28 '14 at 02:52
  • @JenBorn Also, if the answer has helped, feel free to vote to indicate this to other users or hit the green tick if it solved your problem. –  Feb 28 '14 at 02:53