Hi
I am trying to create a webservice that would output either in JSON or XML format depending on the user’s choice.
I problem comes when I am sending a boolean datatype to the user in XML format.
When the datatype is true, it sends the following:
<?xml version='1.0' encoding='UTF-8'?>
<output>
<result>1</result>
</output>
But when the datatype is false, it sends blank value under <result>:
<?xml version='1.0' encoding='UTF-8'?>
<output>
<result></result>
</output>
But it works well when the format is JSON:
In case of true:
{"result":true}
In case of false:
{"result":false}
Here is my code that generates XML or JSON:
$mode = 'XML'; //Change to XML or JSON
$return = array(
'result' => true
);
if('XML' == $mode){
//XML
print "<?xml version='1.0' encoding='UTF-8'?>\
";
print "<output>\
";
foreach ($return as $element => $content) {
print "<$element>";
print $content;
print "</$element>\
";
}
print "</output>";
exit();
}else{
//JSON
echo(json_encode($return) );die;
}
Can someone please help me understand this mystery?
Thanks