Attributes are the things in between an opening and a closing bracket.
<longitude attribute="more info">8.19283</longitude>
You won't find the actual value in the attributeDict
. Parse the longitude/latitude values in parser:foundCharacters:
Edit: Since latitude is first in the XML, also parse it first. To determine whether a value is initial, you should init your location in your location class with some invalid values.
Edit 2: Make sure you have valid XML. NSXMLParser expects ONE! Element that surrounds everything in between. It thought that your XML file was over after </latitude>
.
<location>
<latitude>50.0984321</latitude>
<longitude>-0.13370000</longitude>
</location>
Here is the source that I implemented and tested right now
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"latitude"]) {
self.location = [[Location alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (self.location.isLatitudeInitial) {
self.location.latitude = string.doubleValue;
} else {
self.location.longitude = string.doubleValue;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"longitude"]) {
NSLog(@"%@", self.location);
}
}
And in the location class
#define INITIAL 9999.0
@implementation Location
- (id)init {
self = [super init];
if (self) {
self.latitude = INITIAL;
self.longitude = INITIAL;
}
return self;
}
- (BOOL)isLatitudeInitial {
return self.latitude == INITIAL;
}
- (NSString *)description {
return [NSString stringWithFormat:@"Latitude: %f, longitude: %f", self.latitude, self.longitude];
}
@end
Works perfectly