开发者

how to match nsxml dates from parser and get only the required dates

I am working on an nsxml parser and I have some issues.I am using nsxml parser to parse dates out.I want to get information of only 2 days .not all 4 from google weather api.How can I do it?

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]; 
     }


I'm not familiar with how your XML file is organized but I assume the problem is that there are 4+ nodes with the same name, of which you only want 2. When I ran into a similar situation I set a flag for myself after the last node that I wanted, so I never read anything else.

In your specific case, create a counter for the number of days that you have read, when you are parsing check to make sure that the node name is "date" (or whatever you are currently doing now to trigger reading the information) and also that your counter is less than 2. Just be sure that you reset your counter every time you invoke your weather update.

Edit for some sample code:

- (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
        }
    }
}

This is a piece of my parser, I've kind of rewritten it for you. The part that interests you is the variable 'daysRead'. I would recommend using something like this to keep track of how many days you have already read and stored. If you have more than two, simply don't parse and of the remaining days. Don't forget to increment this after finishing each parsed element as well.

Hope this helps!

-Karoly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜