2012-06-25 65 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。

​​