NSXML 不解析条目的值

发布于 2024-10-16 03:25:18 字数 2893 浏览 2 评论 0原文

我正在构建一个 iPhone 应用程序,它会获取一个 xml 文件,然后解析其中的数据以创建对象。由于某些奇怪的原因,尽管我尝试解析的文本始终具有 nil 值。

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

}

**

I have an iPhone app that I'm building which goes out and gets a xml file and then parses data from it to create objects. For some odd reason though the text I try to parse out always has a value of nil.

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

}

**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2024-10-23 03:25:18

在收到的数据回调中查看此代码:

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

您从未在任何地方设置 propertyValue (我假设它将在 startElement 方法中设置,但事实并非如此),因此您永远不会附加数据。

Look at this code in the data recieved callback :

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

You never set propertyValue anywhere (I assumed that it would be set in the startElement method but it's not) so you never append the data.

淑女气质 2024-10-23 03:25:18

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

在您的 parser:foundCharacters: 消息中尝试以下操作:

    if( self.propertyValue == nil )
    {
        self.propertyValue = [NSString stringWithString:string];
    }

Every time you start an element, you're setting propertyValue to nil. When you find characters, self.propertyValue is nil.

Try this in your parser:foundCharacters: message, instead:

    if( self.propertyValue == nil )
    {
        self.propertyValue = [NSString stringWithString:string];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文