0

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?

SadullahCeran
  • 2,425
  • 4
  • 20
  • 34
  • Try creating the proper NSStringEncoding using `NSStringEncoding iso88599 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin9);` – rmaddy May 05 '13 at 21:15
  • I did. Then replaced unsupported characters with correct ones, but xml parser still gives error. – SadullahCeran May 05 '13 at 21:24

1 Answers1

1

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.

Chris Loonam
  • 5,735
  • 6
  • 41
  • 63