I am needing to parse an XML file for my app and I dont have any clue how to do it. I went through one XMLParser tutorial, and it worked fine but the XML file in the tutoria
i know this isn't the best way...
I struggled for 2 days trying to adapt the XML parser to my situation. I couldnt grasp it, probably because I'm just so used to doing this in C# and obj-c is new to me...
So what I did was parsed the whole thing as a string.
I converted the entire XML file to a NSString, then used substringToIndex and substringFromIndex to isolate the section I needed (the airport). I then used the tag to create an array of , then wrote a for loop that took the values I needed out of the each array object just by getting the range of the tags.
Like I said, it was a crazy solution, but I did it all in 26 lines of code and it works great.
NSString *path = [[NSBundle mainBundle] pathForResource:@"dttps" ofType:@"xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:path];
NSString *xmlString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSRange range = [xmlString rangeOfString:searchedAirport];
xmlString = [xmlString substringFromIndex:range.location];
range = [xmlString rangeOfString:@"/airport_name"];
xmlString = [xmlString substringToIndex:range.location];
NSMutableArray *chartNames = [[NSMutableArray alloc]initWithCapacity:100] ;
NSMutableArray *pdfNames = [[NSMutableArray alloc]initWithCapacity:100] ;
NSArray *charts = [xmlString componentsSeparatedByString:@""];
NSString *tempString = [[NSString alloc]initWithFormat:@""];
int chartsCount = [charts count]-1;
int x;
for (x=0; x < chartsCount; x=x+1) {
tempString = [charts objectAtIndex:x];
range = [tempString rangeOfString:@""];
tempString = [tempString substringFromIndex:range.location+12];
range = [tempString rangeOfString:@" "];
tempString = [tempString substringToIndex:range.location];
[chartNames addObject:tempString];
tempString = [charts objectAtIndex:x];
range = [tempString rangeOfString:@""];
tempString = [tempString substringFromIndex:range.location+10];
range = [tempString rangeOfString:@" "];
tempString = [tempString substringToIndex:range.location-4];
[pdfNames addObject:tempString];
}
followed by cleanup...