2013-11-25 21 views
0

我一直試圖解析下面的XML而沒有任何成功。 這是XML數據: -iOS中XML解析的問題

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

我將此數據轉換爲NSData,然後解析它。 這裏是如何看起來NSData形式: -

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

這是我真正分析數據: -

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

回答

1

屬性是在開啓和關閉支架之間的事。

<longitude attribute="more info">8.19283</longitude> 

attributeDict中找不到實際值。解析parser:foundCharacters:中的經度/緯度值

編輯:由於緯度在XML中是第一個,所以也首先解析它。 要確定某個值是否爲初始值,應該使用一些無效值在您的位置類中初始化您的位置。

編輯2:確保您有有效的XML。 NSXMLParser期待ONE!圍繞着它們之間的所有元素。它認爲你的XML文件在</latitude>之後結束了。

<location> 
    <latitude>50.0984321</latitude> 
    <longitude>-0.13370000</longitude> 
</location> 

這裏是我實現的,現在右測試

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

和在該位置類

#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 

完美的作品

+0

....現在我得到的迴應一樣...經度: - 0.000000 緯度: - 51.509980 – icodes

+0

好像它幾乎工作..現在看我的編輯 – Marc

+0

...甚至奇怪的迴應....經度是: - 9999.000000 – icodes