PHP echo into XML - but need line breaks


I am using PHP to query the database and output the data into XML form so this can be pulled into a flash file.

Unfortunately, it appears that my output does not have any line breaks which is causing it to fail.

echo "<?xml version=\\"1.0\\" encoding=\\"utf-8\\" ?>";
echo "<data>";
echo "  <settings ";
echo 'stageWidth=""	';
echo 'stageHeight=""	';

How do I make each element appear on a new line?

I have tried


but this it think is for HTML not XML

Xml isn’t a text stream, it is a nodelist. Rather than echoing, try creating an xml document using the appropriate bits of php then sending that to the browser.

Thanks wwb_99

I have looked into some examples but my problem is they all directly turn the table structure of the database into the XML file but I need to just cherry pick the required columns - that’s why I was trying to use the echo.

Any pointers on doing this? I’d really appreciate it.

echo PHP_EOL;

Are you sending the appropriate content-type header? If you don’t explicitly send one, the web server will send the default text/html header.

Can you show us the exact code you’re using? I’ll show you how to use the XmlWriter class to output XML in a much neater way.

Try this:

header('Content-Type: text/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\
echo "\	<data>\
echo "\	\	<settings stageWidth=\\"\\" stageHeight=\\"\\">\

will make it appear on a new line. Use \r for Unix-based systems. The \ acts as a tab, which will help keep your code organized. As always, make sure nothing is outputted to the screen before you try to execute header(), otherwise you will get an error.

I have no problem querying the database but I have no idea how to output the result as a formatted XML file that can then be used / pulled into a flash file.

This is the ‘format’ I have to reproduce using data from my database.

<?xml version="1.0" encoding="utf-8"?>

  <!-- LOCATION 1  -->
  <location	x="" y="" landmarkImage="land1" landmarkImageResizeOnZoom="yes" link="" target="_blank" shadow="no">
    <popup image="map_asset/image1.jpg">
    <popupTitle><![CDATA[Interactive Map]]></popupTitle>
    <popupDescription><![CDATA[The main map can be loaded dynamically using xml or created inside the flash itself.]]></popupDescription></popup></location>


The location will obviously be repeated per location - I was using

while($row = mysql_fetch_array($result)){
	echo ' <!-- LOCATION 1  --> ';
	echo ' <location	x="" y="" landmarkImage="land1" landmarkImageResizeOnZoom="yes" link="" target="_blank" shadow="no">';
	echo ' <popup image="map_asset/image1.jpg">';
	echo ' <popupTitle><![CDATA['.$row['name'].']]></popupTitle>';
	echo ' <popupDescription><![CDATA['. $row['price'].']]></popupDescription></popup>';
	echo "</location>";

	echo '</data>';

Actually, whitespace between tags is immaterial in XML. The format is the tags, not what is in between them.

Honestly, not sure what the cool kids are using to make XML in PHP these days. But it ain’t echo.

Not sure if this is “cool” enough:

That would be cool enough.

No, this is wrong. Older Mac systems used \r, I have no idea what they use now - they might be in line with unix now. Unix systems use
, windows uses \r

Thanks for your input everyone.

However, it seems echo does format the XML in a ‘good enough’ format.

The reason my feed was failing in flash was a single ‘white’ space before the XML doc tag. Making sure the echo writing this was placed right at the beginning before any other script fixed the whole issue.

Wait until you have an & in the content . . .

He’s got all the dynamic output wrapped in <![CDATA]> blocks… that takes care of it doesn’t it?

Perhaps yes. On the other hand, you have some urls and other stuff which could easily grow query strings. And better to use a proper library and never have to worry about this stuff rather than treat Xml as a string. Yeah, I’m on the lunatic fringe.