2012-06-25 228 views
0

我是iphone開發人員中的新成員,我對解析知之甚少。但我嘗試了下面的代碼來獲取距離元素文本。我做XML解析以下鏈接:XML解析中的元素

http://maps.googleapis.com/maps/api/directions/xml?origin=30.9165904,75.8634752&destination=30.89314000,75.86938000&sensor=true

請檢查我的代碼,我曾嘗試:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes: 
(NSDictionary *)attributeDict 
{ 

currentElement = [elementName copy]; 
if([currentElement isEqualToString:@"distance"]) 


{ 
     NSLog(@"ENTER IN distance"); 
     text = [[NSMutableString alloc]init]; 

    { 
     dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:text,@"text",nil]; 
    } 

    } 

} 


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 
     if ([currentElement isEqualToString:@"text"]) 
    { 
     [dictionary setObject:text forKey:currentElement]; 
     [text appendString:string]; 
     NSLog(@"text....%@",text); 
    } 
} 


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: 
(NSString *)namespaceURI qualifiedName:(NSString *)qName 

{

if ([elementName isEqualToString:@"distance"]) 
{ 
    [distanparsingarray addObject:dictionary]; 

} 

}

By using above method I am getting the text both of distance and duration. But I want only distance text. Please tell me what I am doing wrong in above code. 

預先感謝。

+0

http://maps.googleapis.com/maps/api/directions/xml?origin=30.9165904,75.8634752&destination=30.89314000,75.86938000&sensor=true – vishal

回答

0

您必須通過以下方式進行管理。 在類級別添加BOOL變量。當您將currentElement標識爲距離時設置它。 僅當上面的BOOL爲true時,發現的內部字符纔會將字符串追加到文本中。 當currentElement不等於distance時,在didEndElement中將此BOOL設置爲false。

​​