首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSXML不解析条目的值

NSXML不解析条目的值
EN

Stack Overflow用户
提问于 2011-02-07 00:55:37
回答 2查看 176关注 0票数 0

我有一个正在构建的iPhone应用程序,它获取一个xml文件,然后解析其中的数据以创建对象。但是,出于某种奇怪的原因,我试图解析出的文本的值总是为nil。

代码语言:javascript
复制
/*
 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];
  }

}

**

EN

回答 2

Stack Overflow用户

发布于 2011-02-07 02:51:20

看看data recieved回调中的代码:

代码语言:javascript
复制
if(nil != self.propertyValue) {
    [self.propertyValue appendString:string];
}

您从来没有设置过propertyValue anywhere (我以为它会在startElement方法中设置,但事实并非如此),所以您永远不会追加数据。

票数 0
EN

Stack Overflow用户

发布于 2011-02-07 03:56:56

每次启动一个元素时,都会将propertyValue设置为零。当您找到字符时,self.propertyValue为空。

请在您的parser:foundCharacters:消息中尝试以下内容:

代码语言:javascript
复制
    if( self.propertyValue == nil )
    {
        self.propertyValue = [NSString stringWithString:string];
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4914717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档