我一直試圖解析下面的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;
}
}
....現在我得到的迴應一樣...經度: - 0.000000 緯度: - 51.509980 – icodes
好像它幾乎工作..現在看我的編輯 – Marc
...甚至奇怪的迴應....經度是: - 9999.000000 – icodes