2011-08-05 3 views
0

나는 nsxml 파서에서 일하고 있으며 몇 가지 문제점이 있습니다. nsxml 파서를 사용하여 날짜를 구문 분석합니다 .2 일만의 정보를 얻고 싶습니다. 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'변수입니다. 나는 당신이 이미 읽고 저장 한 며칠을 추적하기 위해 이와 같은 것을 사용할 것을 권한다. 세 명 이상일 경우에는 남은 날을 파싱하지 마십시오. 각 구문 분석 된 요소를 완료 한 후에이 값을 증가시키는 것을 잊지 마십시오.

희망이 도움이됩니다.

-Karoly

+0

파서의 파일을 nsdictionary에 저장합니다. – iphone

+0

구문 분석 할 실제 XML 형식을 언급했습니다. 그럼에도 불구하고 플래그/카운터를 만들고 단순히 구문 분석하지 않는 것이 문제를 해결해야합니다. –

+0

당신은 나에게 더미 코드를 보여줄 수 있습니까? 나는 그물에 어떤 예를 찾을 수 없습니다. 나는 매우 새로운 목적 c에 있습니다. – iphone

관련 문제