iOS中XML解析的问题

问题描述:

我一直试图解析下面的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; 
} 
} 

属性是在开启和关闭支架之间的事。

<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

您是否尝试过使用AFNetworking框架的来源? 在这里你可以找到我刚才你所提到的例子http://www.raywenderlich.com/30445/afnetworking-crash-course