Question

I have been trying to parse the below XML without any success. This is the XML data :-

 <latitude>51.50998000</latitude> <longitude>-0.13370000</longitude>

I convert this data to NSData and then parse it . Here is how it looks in NSData form :-

presence converted to NSData :- <3c6c6174 69747564 653e3531 2e353039 39383030 303c2f6c 61746974 7564653e 203c6c6f 6e676974 7564653e 2d302e31 33333730 3030303c 2f6c6f6e 67697475 64653e>

This is how i actually parse the data :-

 -(LocationParser *)initXMLParser:(NSData *)dataWithlocation
{
    self=[super init];
    if (self){
    self.receivedData = dataWithlocation;
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:self.receivedData];
    [parser setDelegate:self];
    BOOL success=[parser parse];
    if (success) {
        NSLog(@"Successful Parsing");

    }
    else{
        NSLog(@"Error while parsing");
    }
}
return self;
}

 -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
 {
   if ([elementName isEqualToString:@"latitude"])
 {
   _location = [[Location alloc]init];

    //Extract the attribute here
    _location.latitude =[[attributeDict objectForKey:@"latitude"]stringValue];
    NSLog(@"latitude is :- %@", _location.latitude);  // this shows latitude is :- (null)
    _location.longitude = [[attributeDict objectForKey:@"longitude"]stringValue];
     NSLog(@"longitude is :- %@", _location.longitude); // this shows  longitude is :- (null)

  }
 }

   - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
   {
    if(!_curElem)
    _curElem = [[NSMutableString alloc] initWithString:string];
  else
    [_curElem appendString:string];

  NSLog(@"Processing Value: %@", _curElem);  //This shows Processing Value: 51.50998000

}


    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
   {
     if ([elementName isEqualToString:@"latitude"])
   {
    return;

   }
    else
   {
    [_location setValue:_curElem forKey:elementName];

    _curElem = nil;
}
}
Was it helpful?

Solution

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

OTHER TIPS

Have you tried using AFNetworking framework? Here you can find an example http://www.raywenderlich.com/30445/afnetworking-crash-course

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top