2011-08-05 3 views
0

私はnsxmlパーサーに取り組んでおり、いくつかの問題があります.nsxmlパーサーを使って日付を解析しています。 Google天気予報apiからすべての4つ。私はそれを行うことはできますか?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ファイルの編成方法に慣れていないんだけど、私は、問題は、私は似たような状況に遭遇したときにのみ2を表示したいのと同じ名前を持つ4+のノードが存在するということであると仮定私は自分が望んでいた最後のノードの後に​​自分自身のために旗を立てました。

特定のケースでは、読み込んだ日数のカウンタを作成し、解析するときにノード名が「日付」であることを確認します(または、情報)とカウンターが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'です。すでに読んで保存した日数を把握するために、このようなものを使用することをお勧めします。 3つ以上ある場合は、残りの日数を解析しないでください。解析された各要素を終了した後にこれをインクリメントすることを忘れないでください。

希望すると便利です。

-Karoly

+0

私はパーサからnsdictionaryにファイルを保存しています。 – iphone

+0

私はあなたが解析している実際のXMLのフォーマットを参照していました。それにかかわらず、フラグ/カウンタを作成し、必要以上に解析しないだけで、問題を解決するはずです。 –

+0

あなたは私にダミーコードを見せることができます...私はネット上の例を見つけることができません。私は非常に非常に新しい目的cです。 – iphone

関連する問題