Jump to content

print array in PHP, returned as XML data


socialmatrix

Recommended Posts

My script has this on the top.

header('Content-Type: text/xml');
echo "<?xml version='1.0' encoding='UTF-8'?>";

 

Then, I have following function.

function addItCall($request)
{
	$retval = "<value>";
	$retval .= $_GET['num1'] + $_GET['num2'];
	$retval .= "</value>";
	echo "$retval";
}

This works fine and, returns result as XML data.

 

The next function is

function printNumbersCall($request)
{
	$i=1;
	while($i<=5)
	{
		$retval .= "<number>" . $i . "</number>";
		$i++;
	}
	echo "$retval";
}

However, this won't return 1-5 in XML format.

 

I searchEngined, and this is the only help I found. http://simonwillison.net/2003/Apr/29/xmlWriter/ is this the only way to handle this? it seems complicated.

Link to comment
https://forums.phpfreaks.com/topic/119198-print-array-in-php-returned-as-xml-data/
Share on other sites

function printNumbersCall($request)

{

$i=1;

while($i<=5)

{

echo "<number>" . $i . "</number>\n";

$i++;

}

}

 

Give that a go.  But it might not work.  Do you have a single element that wraps the whole file?

<numbers>

  <number>1</number>

  <number>2</number>

  etc...

</numbers>

 

Well-formed XML requires it.

Hi, DarkWater

 

This worked :). I completely didn't think of the requirement of well-formed XML.

function printNumbersCall($request)
{
	$retval = "<numbers>";
	$i=1;
	while($i<=5)
	{
		$retval .= "<number>" . $i . "</number>";
		$i++;
	}
	$retval .= "</numbers>";
	echo "$retval";
}

 

Thanks a lot,

Atit

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.