0

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;
}
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

I did what you have mentioned ....and now i am getting a response like ...longitude is :- 0.000000 latitude is :- 51.509980
actually ..i get this...latitude is :- 51.509980 using if (self.location.isLatitudeInitial) { self.location.latitude = string.doubleValue; NSLog(@"latitude is :- %f", _location.latitude);}
yes but i am getting only latitude value...the longitude value is still not displayed.
See my edit. Your problem was not only in the parsing but also in the XML file
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.