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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.