[英]NSXML Not parsing values for entries
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. 我有一个正在构建的iPhone应用程序,该应用程序退出并获取xml文件,然后从其中解析数据以创建对象。 For some odd reason though the text I try to parse out always has a value of nil. 由于某些奇怪的原因,尽管我尝试解析的文本始终具有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];
}
}
** **
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. 您永远不会在任何地方设置propertyValue(我假设它将在startElement方法中设置,但是没有设置),因此您永远不会追加数据。
Every time you start an element, you're setting propertyValue to nil. 每次启动元素时,都将propertyValue设置为nil。 When you find characters, self.propertyValue is nil. 找到字符时,self.propertyValue为零。
Try this in your parser:foundCharacters:
message, instead: 在您的parser:foundCharacters:
消息中尝试此操作,而不是:
if( self.propertyValue == nil )
{
self.propertyValue = [NSString stringWithString:string];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.