IOS Encoding ISO-8859-9 - ios

I need to get contents of an XML file which is hosted on web, and parse it.
I decided to use TouchXML for parsing process. However, I cannot get contents of the file since it is encoded with ISO-8859-9
XML File Url: http://rss.haberler.com/mobil/sondakika2.asp?kategori=manset
I have tried 2 different approachs.
1) Getting contents of url into NSString:
NSString *url = #"http://rss.haberler.com/mobil/sondakika2.asp?kategori=manset";
NSError *error = nil;
NSStringEncoding encoding;
NSString *xmlString = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:url] usedEncoding:&encoding error:&error];
xmlString becomes null, and error description says:
The operation couldn’t be completed. (Cocoa error 261.)
Instead of usedEncoding, I also tried specifying encoding explicitly, from UTF-8 to NSISOLatin1StringEncoding and NSISOLatin2StringEncoding (unfortunately, I could not find NSISOLatin9StringEncoding).
2) I also tried to load xml into NSData.
NSError *error = nil;
NSData *XMLData = nil;
XMLData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:&error];
When I constructed XML Parser, specific characters are unknown and application terminates when I get string values.
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:XMLData options:0 error:nil];
NSArray *nodes = [doc nodesForXPath:#"//item" error:nil];
for (CXMLElement *node in nodes) {
for(int counter = 0; counter < [xmlElement childCount]; counter++) {
CXMLNode * child = [xmlElement childAtIndex:counter];
NSString *childName = child.name;
NSString * childValue = [child stringValue];
}
}
Getting stringValue of child terminates application with SIGABRT.
How can I fix the problem?

From the TouchXML CXMLDocument.m file, this is what initWithData: looks like
- (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError
{
return [self initWithData:inData encoding:NSUTF8StringEncoding options:inOptions error:outError];
}
What you could try doing is, in this file, replace NSUTF8StringEncoding with NSASCIIStringEncoding or whatever encoding it uses. That may fix it.

Related

Unable to Convert XMLData into Dictionary Form

I have an XML file and I am converting the data into NSString using this code:-
NSString *strPath=[[NSBundle mainBundle] pathForResource:#"VISG" ofType:#"xml"];
NSData *data = [NSData dataWithContentsOfFile:strPath];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
The problem I am facing is I am getting the string with all the XMLValues but I am unable to convert that string into Dictionary. I have used XMLReader but it always returns me nil . I have tried the below two methods for converting the string which I ma getting from NSdata but it always throws nil
dict = [XMLReader dictionaryForXMLString:string
options:XMLReaderOptionsProcessNamespaces
error:&error];
dict = [XMLReader dictionaryForXMLString:string error:nil]
Can anyone please tell me what is the problem. Or can you please tell me some more libraries other than XMLReader for parsing the XMLfile.

Problems with fetching data from http in iOS 9

I'm trying to fetch data in iOS 9 like this:
NSError *error = nil;
NSData *avaliableVersion = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://energyapp.co/fonts/vers.php"] options:0 error:&error];
NSString *str = [[NSString alloc]initWithData:avaliableVersion encoding:NSUTF8StringEncoding];
NSLog(str);
As a result, it loads very long time, but here is just few symbols. And result in "str" is empty.

Unable to read/parse a local json file

I am trying to parse a json file but I am unable to read/parse the data from the file. I prepared the The json data file by copying the contents from a web service call. I copied the contents and saved it in the projects directory. The path to the file is correct.
NSError *error;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"js"];
NSLog(#"Path: %#", filePath);
// NSInputStream *fileInputStream = [[NSInputStream alloc] initWithFileAtPath:filePath];
// [fileInputStream open];
// NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithStream:fileInputStream options:0 error:&error];
// NSLog(#"Stream: %#", jsonData);
NSData *fileContents = [[NSData alloc] initWithContentsOfFile:filePath];
NSLog(#"Data: %#", fileContents);
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:fileContents options:kNilOptions error:&error];
NSLog(#"Dictionary: %#", jsonData);
The "fileContents" prints like "20202020 20202020 20202063 6c756249 64203d20" (may be it is hexadecimal value). Why am I getting the data like this? When I parse same date by calling the web service, it works fine.
I have left some commented code here to let you know that I have tried that portion as well.
Any help is much appreciated!!

NSString initWithContentsOfURL problems

I am updating an iOS app to be a good boy and clean out some deprecated methods. I have been using NSString initWithContentsOfURL and I am trying to implement the new version initWithContentsOfURL:usedEncoding:error:.
I have used the following in my view controller:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:#"http://myurl.com/directory/file.txt"];
NSError *error;
NSStringEncoding *encoding = NULL;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:encoding
error:NULL];
NSLog(#"string:%#", tryString);
[super viewDidLoad];
}
But I keep getting a thread break at the error:NULL point. I have tried error:nil and using a pointer to the predefined error object as nil or NULL but same thing happens. Has anyone any ideas or tips.
NSError *error = nil;
NSStringEncoding encoding = 0;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Both the error and encoding should be pass like this:
NSError *error = nil;
NSStringEncoding encoding = nil;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];

iOS - How to get more debugging information on nil return values

In my iOS 5.1 application, I'm trying to load a JSON text file into a NSDictionary.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"stuff" ofType:#"json"];
NSDictionary *stuff = [NSDictionary dictionaryWithContentsOfFile:filepath];
The filepath is valid, the file exists, but stuff is nil, which means there was a problem decoding the file (according to the documentation).
(How) Can I get more debugging information on why [NSDictionary dictionaryWithContentsOfFile:] - or any kind of API call - returns nil?
Read the contents as NSData and then use +[NSPropertyListSerialization propertyListWithData:options:format:error:].
As you said: Your file contains a JSON string. First, you have to load the string and then you have to parse it using some JSON library.
E.g.:
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString: content];

Resources