2011-08-05 50 views
0

我正在使用nsxml解析器,并且我遇到了一些问题。我使用nsxml解析器来解析日期。我想仅获取2天的信息.not所有4个来自谷歌天气api.How我可以做到吗?如何匹配解析器的nsxml日期并获取所需的日期

NSMutableArray *array=[NSMutableArray arrayWithObjects:startDate,endDate,Nil]; 
for (NSDate *date in array) 
{ 
    if([date isEqualToDate:dd]) 
    { 
     NSManagedObject *allnew = Nil; 
     NSManagedObjectContext *allone=[self managedObjectContext];    
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Weather" inManagedObjectContext:allone]; 
     NSLog(@" The NEW ENTITY IS ALLOCATED AS entity is %@",entity); 
     WeatherXMLParser *delegate = [[WeatherXMLParser alloc] initWithCity:city state:state country:country]; 
     NSXMLParser *locationParser = [[NSXMLParser alloc] initWithContentsOfURL:delegate.url]; 
     [locationParser setDelegate:delegate]; 
     [locationParser setShouldResolveExternalEntities:YES]; 
     [locationParser parse]; 
     NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
     [request setEntity:entity]; 

     predicate = [NSPredicate predicateWithFormat: 
            @"city == %@ and state == %@ and country == %@ and date==%@ and date==%@", city, state, country,startDate,endDate]; 
     [request setPredicate:predicate]; 
     NSError *err; 
     NSUInteger count = [allone countForFetchRequest:request error:&err]; 
     NSLog(@" minimum salary is %@",predicate); 
     // If a predicate was passed, pass it to the query 
     if(predicate !=NULL){ 
       [self deleteobject]; 
     } 

     Weather *weather = (Weather *)[NSEntityDescription insertNewObjectForEntityForName:@"Weather" 
                        inManagedObjectContext:self.managedObjectContext]; 
     weather.date = [fields objectForKey:@"date"]; 
     weather.high =[fields objectForKey:@"high"]; 
     weather.low = [fields objectForKey:@"low"]; 
     weather.city =[fields objectForKey:@"city"]; 
     weather.state =[fields objectForKey:@"state"]; 
     weather.country =[fields objectForKey:@"country"]; 
     NSString*icon=[fields objectForKey:@"icon"]; 
     NSString *all=[icon lastPathComponent]; 
     weather.condition = all; 

     [self saveContext]; 
    } 

回答

0

我不熟悉如何XML文件的组织,但我认为问题是,有4+节点具有相同的名称,而你只想要2。当我遇到了类似的情况在我想要的最后一个节点之后,我为自己设置了一个标志,所以我从不读其他任何东西。

在您的具体情况下,为您读取的天数创建一个计数器,在解析时检查以确保节点名称是“日期”(或者您现在正在执行的任何操作,以触发读取信息),并且您的柜台还不到2个。确保每次调用天气更新时重置计数器。

编辑了一些示例代码:

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

    if ([elementName isEqualToString:@"Day"] && daysRead < 2) {  
     if ([[attributeDict objectForKey:@"high"] integerValue] > 0) { 
      //Store store this value 
     } 
     if ([[attributeDict objectForKey:@"low"] integerValue] > 0) { 
      //Store this value 
     } 
    } 
} 

这是我的一块解析器,我有种改写它。您感兴趣的部分是变量'daysRead'。我建议使用这样的东西来跟踪你已经读取和存储了多少天。如果你有两个以上的人,就不要分析和剩下的日子。不要忘记在完成每个解析元素之后增加它。

希望这会有所帮助!

-Kolyoly

+0

我将文件从解析器存储到nsdictionary中。 – iphone

+0

我指的是您正在解析的实际XML格式。无论如何,创建一个标志/计数器,而不是解析超过您的需要应该解决您的问题。 –

+0

你能告诉我虚拟代码吗...我无法在网上找到任何例子。我在客观的c中非常新。 – iphone

相关问题