SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Blank response in XML WebService

    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:
    Code:
    <?xml version='1.0' encoding='UTF-8'?> 
    <output> 
     <result>1</result> 
    </output>

    But when the datatype is false, it sends blank value under <result>:
    Code:
    <?xml version='1.0' encoding='UTF-8'?> 
    <output> 
     <result></result> 
    </output>

    But it works well when the format is JSON:

    In case of true:
    Code:
    {"result":true}
    In case of false:
    Code:
    {"result":false}


    Here is my code that generates XML or JSON:

    Code:
    			
    			$mode = 'XML'; //Change to XML or JSON
    			
                $return = array(
                    'result' => true
                );		
                	
                if('XML' == $mode){
    		        //XML	
    		        print "<?xml version='1.0' encoding='UTF-8'?>\n";
    		        print "<output>\n";
    		        foreach ($return as $element => $content) {
    		           print "<$element>";
    		           print $content;
    		           print "</$element>\n";
    		        }
    		        print "</output>";
    		        exit();  
    		                  
                }else{
    				//JSON
    				echo(json_encode($return) );die;            
                }


    Can someone please help me understand this mystery?


    Thanks

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Let's do a little exercise: Please run the following code and have a look at what happens:

    Code PHP:
    echo true;
    echo false;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •