I am trying to read an XML file using objective C language and parse it. I am using XCode 4.2 and writing application for iPhone. I have created Viewcontroller .h and .m files. I am using NSXMLParser and xml file is named as “simple.xml”. I have downloaded it from w3schools website. I am not using ARC. There are no build errors.
Below is my code in viewDidLoad method –
NSString *filePath;
filePath = [[NSBundle mainBundle] pathForResource:@"simple" ofType:@"xml"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *urlString = [[NSString alloc] initWithData:fileData encoding:NSASCIIStringEncoding];
NSLog(@"File data is %@",urlString);
NSString* str= urlString;
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
xmlParser = [[NSXMLParser alloc] initWithData:data];
xmlParser.delegate = self;
[xmlParser parse];
Here is the XML content –
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
But when I execute this code the program terminates abruptly. I do not see any exception info in logs. But it has something to do with fileData variable because in XCode the line in which I am using this variable is shown in green color with text “thread 1 stopped executing…”. Perhaps the XML is encoded in ISO-8859-1 and I am using ASCII encoding? Earlier I used similar code for parsing XML returned from a web-service and at that time response was encoded in UTF-8. Or maybe I have made some mistake in memory management..allos, release etc? Kindly help me in this.
Also I tried saving the file with “UTF-8” in “TextEdit” but there was no such option available.
removed encoding part and passed whatever data I read from the xml file directly to the XML parser and it worked for me.