www.webdeveloper.com
Results 1 to 4 of 4

Thread: Blank response in XML WebService

  1. #1
    Join Date
    Sep 2006
    Posts
    632

    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
    Last edited by phantom007; 05-05-2013 at 10:46 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    json_encode() has built-in logic to detect Boolean values and convert them into the string representation used for JSON. In PHP, however, when you echo a Boolean, it does an implicit type cast to a string (since that's all you can really print/echo), for which true becomes "1" and false becomes "". You might want to make a little function to detect Booleans and output whatever you want in such a case.
    PHP Code:
    <?php
    function booleanToStr($value)
    {
        if(
    is_bool($value)) {
            
    $value = ($value) ? 'true' 'false';
        }
        return 
    $value;
    }

    function 
    booleanToInt($value)
    {
        if(
    is_bool($value)) {
            
    $value = (int) $value;
        }
        return 
    $value;
    }

    // example
    $foo true;
    $bar false;
    $fubar 'text';

    echo 
    "<p>".booleanToStr($foo)." | ".booleanToStr($bar)." | ".booleanToStr($fubar)."</p>";
    echo 
    "<p>".booleanToInt($foo)." | ".booleanToInt($bar)." | ".booleanToInt($fubar)."</p>";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2006
    Posts
    632
    Hi Thanks for the reply. Will it be a good idea to use something like this?


    if('XML' == $mode){
    //XML
    print "<?xml version='1.0' encoding='UTF-8'?>\n";
    print "<output>\n";
    foreach ($return as $element => $content) {
    print "<$element>";
    if($content){
    echo 'true';
    }else{
    echo 'false';
    }
    print "</$element>\n";
    }
    print "</output>";
    exit();

    }else{
    //JSON
    echo(json_encode($return) );die;
    }

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    It's probably fine for a single use like that. However, if you're going to end up doing the same thing in several places, I'd define a function something like one of those I wrote above so that you can follow the DRY principle (Don't Repeat Yourself).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles