socialmatrix Posted August 11, 2008 Share Posted August 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
tibberous Posted August 11, 2008 Share Posted August 11, 2008 Why do you need it as XML? Not a lot of things require / use <?xml version='1.0' encoding='UTF-8'?>, are you converting it to XML for the sake of it? Quote Link to comment Share on other sites More sharing options...
socialmatrix Posted August 11, 2008 Author Share Posted August 11, 2008 @DarkWater: Thank you, let me try that. I'll post here again. Thanks @tibberous: For this project, I build RESTful web services for other students. It should return well-formed XML, so they can parse and it as they need. Quote Link to comment Share on other sites More sharing options...
socialmatrix Posted August 11, 2008 Author Share Posted August 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 No problem. You may want some \n in there too so it looks nice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.