2011-02-06 7 views
0

ビルド中のiPhoneアプリがあり、XMLファイルを取得してデータを解析してオブジェクトを作成しています。何か奇妙な理由のために、私が解析しようとするテキストは常にゼロの値を持っています。NSXMLエントリの値を解析していません

/* 
Sample entry 
<course> 
<name>Afton Alps Golf Course</name> 
<address>6600 Peller Ave S, Hastings, MN 55033</address> 
<desc>This is only a test</desc> 
<rating>69</rating> 
<location>42.12452 -90.53 </location> 
<course> 
*/ 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict { 
    if(nil != qName) { 
    elementName = qName; // swap for the qName if we have a name space 
    } 

    if ([elementName isEqualToString:@"course"]) { 
    self.currentCourse = [[[Course alloc] init] autorelease]; 
    } else { 
     self.propertyValue = nil; 
    } 
} 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if(nil != self.propertyValue) { 
    [self.propertyValue appendString:string]; 
    } 
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {  
    if (qName) { 
    elementName = qName; // switch for the qName 
    } 
    if ([elementName isEqualToString:@"name"]) { 
     self.currentCourse.name = self.propertyValue; 
    } else if ([elementName isEqualToString:@"address"]) { 
     self.currentCourse.address = self.propertyValue; 
    } else if ([elementName isEqualToString:@"desc"]) { 
     self.currentCourse.description = self.propertyValue; 
    } else if ([elementName isEqualToString:@"rating"]) { 
     NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; 
     [f setNumberStyle:NSNumberFormatterDecimalStyle]; 
     self.currentCourse.rating = [f numberFromString: self.propertyValue]; 
     [f release]; 
    } else if ([elementName isEqualToString:@"location"]) { 
     NSArray *comp = [self.propertyValue componentsSeparatedByString:@" "]; 
     NSLog(@"%s", [comp objectAtIndex:0]); 
     NSLog(@"%s", [comp objectAtIndex:1]); 
     NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
     NSNumber *latitude = [formatter numberFromString:[comp objectAtIndex:0]]; 
     NSNumber *longitude = [formatter numberFromString:[comp objectAtIndex:1]]; 
     [formatter release]; 
     CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.floatValue 
                 longitude:longitude.floatValue]; 
     self.currentCourse.location = location; 
     [location release]; 
    } else if([elementName isEqualToString:@"course"]) { 
     NSLog(@"We're done"); 
     [(id)[self delegate] performSelectorOnMainThread:@selector(addCourse:) 
              withObject:self.currentCourse 
             waitUntilDone:NO]; 
    } 

} 

**

答えて

0

コールバックを受け取ったデータでこのコードを見てみましょう。

if(nil != self.propertyValue) { 
    [self.propertyValue appendString:string]; 
} 

あなたはどこにでもPropertyValueを設定しません(私はそれがのstartElementメソッドで設定されることを想定したがそうではありません)ので、データを追加することはありません。

0

要素を開始するたびに、propertyValueをnilに設定します。文字を見つけると、self.propertyValueはnilになります。

代わりに、あなたのparser:foundCharacters:メッセージでこれを試してみてください:

if(self.propertyValue == nil) 
    { 
     self.propertyValue = [NSString stringWithString:string]; 
    } 
関連する問題