The parsing of a file containing XML breaks when ever there is a string containing '?'. As an example you can see the line below.
<Radio id="32">
<stationName>BBC 5 Live</stationName>
<streamType>aac+</streamType>
<streamBandwidth>48kbps</streamBandwidth>
<streamURL>http://bbcmedia.ic.llnwd.net/stream/bbcmedia_he2_5live_q?s=1308038932&e=1308053332&h=868e4fa343b375695183f6a3bd0267d9</streamURL>
</Radio>
Is there some way to encode the '?' or what is the way thats generally used to handle this kind of problem, as I would imagine this would be encountered a lot.
The line of code that handles this (i believe) is :
[aRadio setValue:currentElementValue forKey:elementName];
Though I maybe that is not where it breaks.
Many Thanks,
-Code
Most probably, the problem is not the '?' character, but the '&' characters in your URL. The '&' character has a special meaning in XML (it is used to start entities like & or <), so the XML parser will fail if it doesn't find a valid entity. The way to go is to wrap the value in a CDATA section as deanWombourne suggests.
Try this. Convert it into UTF8
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
Then parse with this data...
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
There's a few ways of dealing with this.
1) Percent encode the string before sending from the server (your '?' would become '%3F') and decode the data when you receive in in your app. - see this answer for more details.
2) Use CDATA markers around this bit of data - see here for more details
Related
Is there a way to parse an XML in iOS where the attribute are not separated
e.g:
Users
UserId="1" Name="John Smith" Loc="London"
UserId="2" Name="Johnny Cash" Loc="Nashville"
Users
Thanks
It seams like you havent got xml at all. You are missing all usefully symbols that would normally help with the parsing. You taks is to parse a new format specification.
My first bit of advice is to ask whoever is providing you with this feed to put it into a proper format (JSON or plist are the easiest to work with).
Failing this, if the feed is not too big (otherwise you will hit performance issues), parse the feed manually character by character. You probably want to write a event based parser.
Split the feed line by line, perhaps using componentsSeparatedByString:
Then read characters into a string untill you hit an = that string is your key. Next read between the quotes "" That string is your value. FIre the key and the value off to a delegate.
JSON parsing classes will help you out...
NSString *responseString = #""; // your data contained string.
SBJSON *json = [[SBJSON new] autorelease];
NSArray *resultData = [json objectWithString:responseString error:&error];
i am using uiwebview in my application. there are some links when user clicks a http search starts. it works fine but i have problems while getting "%58 den ysnky'ye tepki" it is given as "X'den ysnky'ye tepki". it has problems with % char.
identifier:%58'den%20ysnky'ye%20tepki
decoded identifier:X'den ysnky'ye tepki
i am using stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding to decode the string like that;
NSLog(#"identifier:%#", identifier);
identifier = [identifier stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"decoded identifier:%#", identifier);
how can i get the correct string?
thanks...
It looks like your string may not be encoded properly in the first place. %58 is the correct encoding for the letter “X” (see this ASCII table). As far as I can tell, therefore, the decode is behaving properly.
What are you expecting?
I have some image data (jpeg) I want to send from my iPhone app to my webservice. In order to do this, I'm using the NSData from the image and converting it into a string which will be placed in my JSON.
Currently, I'm doing this:
NSString *secondString = [[NSString alloc] initWithBytes:[result bytes]
length:[result length]
encoding:NSUTF8StringEncoding];
Where result is of type NSData. However, secondString appears to be null even though result length returns a real value (like 14189). I used this method since result is raw data and not null-terminated.
Am I doing something wrong? I've used this code in other areas and it seems to work fine (but those areas I'm currently using it involve text not image data).
TIA.
For binary data, better to encode it using Base64 encoding then decode it in you webservice. I use NSData+Base64 class downloaded from here, this reference was also taken from Stackoverflow, an answer made by #Ken (Thanks Ken!).
You are not converting the data to a string. You are attempting to interpret it as a UTF-8 encoded string, which will fail unless the data really is a UTF-8 encoded string. Your best bet is to encode it somehow, perhaps with Base64 as Manny suggests, and then decode it again on the server.
I am aware this question has been asked several times, but I was unable to find a definate answer that would best fit my situation.
I want the ability to have the user select an image from the library, and then that image is converted to an NSData type. I then have a requirement to call a .NET C# webservice via a HTTP get so ideally I need the resulting string to be UTF8 encoded.
This is what I have so far:
NSData *dataObj = UIImageJPEGRepresentation(selectedImage, 1.0);
[picker dismissModalViewControllerAnimated:YES];
NSString *content = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];
NSLog(#"%#", content);
The NSLog statement simply produces output as:
2009-11-29 14:13:33.937 TestUpload2[5735:207] (null)
Obviously this isnt what I hoped to achieve, so any help would be great.
Kind Regards
You can't create a UTF-8 encoded string out of just any arbitrary binary data - the data needs to actually be UTF-8 encoded, and the data for your JPEG image obviously is not. Your binary data doesn't represent a string, so you can't directly create a string from it - -[NSString initWithData:encoding:] fails appropriately in your case.
Assuming you're using NSURLConnection (although a similar statement should be true for other methods), you'll construct your NSMutableURLRequest and use -setHTTPBody: which you need to pass an NSData object to. I don't understand why you would be using a GET method here since it sounds like you're going to be uploading this image data to your web service - you should be using POST.
How do I use comma-separated-values received from a URL query in Objective-c?
when I query the URL I get csv such as ("OMRUAH=X",20.741,"3/16/2010","1:52pm",20.7226,20.7594).
How do I capture and use this for my application?
You have two options:
Use a CSV parser: http://freshmeat.net/projects/ccsvparse
Or parse the data yourself into an array:
// myString is an NSString object containing your data
NSArray *array = [myString componentsSeparatedByString: #","];
I recently dealt with CSV parsing for Yahoo! Finance as well. I used Ragel to write a parser in C that was good enough for the CSV I was getting. It handled everything but escaped quotes, which are not going to show up much in stock quotes. It was pretty painless and a good learning experience. I'd post the code, but it was work-for-hire, so I don't own it.
Turning a C string into an NSString is easy. If you have it as an NSData, as you likely do at the end of a URL download, just do [[NSString alloc] initWithData:csvData encoding:NSUTF8StringEncoding]. If you have a pointer to a character buffer instead, use [[NSString alloc] initWithBytes:buffer length:buflen encoding:NSUTF8StringEncoding]. buflen could be strlen(buffer) if buffer is a normal, NUL-terminated C string.