Skip to main content

Sending encoded XML to a server from iPhone



I'm communicating between an iPhone app and a server using XML. To get around the problem of the user entering any character that might break the XML before I send it to the server i'm using the NSString method:







[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];







But I noticed when doing a log of the string that the & symbol is not escaped? I thought that it should be? spaces, quotation marks and hash symbols which I've tested for all encode fine but not the ampersands?





What is the most common methods used for encoding/decoding XML sent to/received from a server?





Thanks.


Comments

  1. The answer provided by @sleepy is a good approach, or if you prefer the syntax of implementing this in a category see this link for an example.

    Note that you should need to do this only for characters you are sending to the server via the URL parameters. If you are actually POST'ing XML data to the service by placing the XML content into the HTTP body of your request, then this kind of encoding is not needed. Depending on the size of the XML to be posted and the interface supported by your service, using the HTTP body might be a better choice in some scenarios.

    As an example of how to do this in the HTTP body, I typically use something like this:

    NSData *requestData = [requestXml dataUsingEncoding:NSUTF8StringEncoding];
    NSString *requestLength = [NSString stringWithFormat:@"%d", [requestXml length]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serviceURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
    [request setValue:requestLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:requestData];


    where requestXML is an NSString containing the XML I want to post and serviceURL is an NSURL for the service URL. In this case I am using UTF encoding for my XML content, which is what you should probably be using in all cases unless you have a specific reason not to.

    ReplyDelete
  2. I assume you are POSTing the data. You can use the following function for escaping the XML data:

    +(NSString *)urlEncode:(NSString *)str {
    return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
    (CFStringRef)str,
    NULL,
    (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
    CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.