Bennie
Bennie

Reputation: 509

I can't get asXml to return like it should

I have some straightforward code, which should return an xml string, but when i run this it returns only:'132963660013292910001330196400' which is all three of the second chile of all three concerts concatenated together. This is literally copied straight out of a textbook so i don't see how i could be doing it wrong to get this result. What am I misunderstanding here?

$simplexml = new SimpleXMLElement(
'<?xml version="1.0"?><concerts />');
$concert1 = $simplexml->addChild('concert');
$concert1->addChild("title", "The Magic Flute");
$concert1->addChild("time", 1329636600);
$concert2 = $simplexml->addChild('concert');
$concert2->addChild("title", "Vivaldi Four Seasons");
$concert2->addChild("time", 1329291000);
$concert3 = $simplexml->addChild('concert');
$concert3->addChild("title", "Mozart's Requiem");
$concert3->addChild("time", 1330196400);
echo $simplexml->asXML();

/* SHOULD output:
<concerts><concert><title>The Magic Flute</title><time>1329636600➥
</time></concert><concert><title>Vivaldi Four Seasons</title>➥
<time>1329291000</time></concert><concert><title>Mozart's Requiem➥
</title><time>1330196400</time></concert></concerts>
*/

Upvotes: 1

Views: 230

Answers (1)

Phil
Phil

Reputation: 164911

Sounds like you're viewing the output of your script via a browser and it's attempting to interpret the document as HTML (the default content-type for PHP scripts).

Add this anywhere before your last line (the echo line)

header('Content-type: text/xml');

Alternatively, if you would like to see this as an HTML document, try this one

echo '<pre>', htmlspecialchars($simplexml->asXML()), '</pre>';

Demo here - http://codepad.viper-7.com/BJ2adH

Upvotes: 1

Related Questions