我试图从xml web源获取内容列表。我正在使用NSXMLParser atm。
这是代码:
- (void)main
{
self.workingArray = [NSMutableArray array];
self.workingPropertyString = [NSMutableString string];
NSURL *url = [[NSURL alloc]initWithString:@"http://myxmlwebsite.xml"];
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
[parser setDelegate:self];
bool result = [parser parse];
NSLog(@"result is ok for xml parse : %@", result ? @"Yes" : @"No");
if (![self isCancelled])
{
self.appRecordList = [NSArray arrayWithArray:self.workingArray];
SubCategoryViewController *subCategoryViewController;
subCategoryViewController.entries = self.appRecordList;
[subCategoryViewController.tableView reloadData];
}
self.workingArray = nil;
self.workingPropertyString = nil;
self.dataToParse = nil;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:kUser])
{
self.workingEntry = [[UserFetchAppRecord alloc] init];
}
_elementsToParse = [[NSArray alloc] initWithObjects:
kid,ktitle, nil];
self.storingCharacterData = [_elementsToParse containsObject:elementName];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if (self.workingEntry)
{
if (self.storingCharacterData)
{
NSString *trimmedString = [self.workingPropertyString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.workingPropertyString setString:@""];
if ([elementName isEqualToString:kid])
{
self.workingEntry.ids = trimmedString;
NSLog(@"id : %@" , trimmedString);
}
else if ([elementName isEqualToString:ktitle])
{
self.workingEntry.title = trimmedString;
NSLog(@"ktitle : %@" , trimmedString);
}
}
else if ([elementName isEqualToString:kUser])
{
NSLog(@"inside elementName isEqualToString:kUser");
[self.workingArray addObject:self.workingEntry];
NSUInteger self_workingArrayCount = [self.workingArray count];
self.workingEntry = nil;
}
}
}
现在在控制台,如果我运行程序,我得到这些结果:
“结果可以用于xml parse = true”
“id:1”
“ktitle:usertitle”
。 。
但是关闭选项卡的结果没有显示在控制台中,这个“在elementName内部是isEqualToString:kUser”。
怎么可能?
我的xml标签是这样的:
<Main>
<user>
<id></id>
<title></title>
</user>
<user>
<id></id>
<title></title>
</user>
.....
</Main>
答案 0 :(得分:1)
更改
中的else if ([elementName isEqualToString:kUser])
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
到if ([elementName isEqualToString:kUser])
。
self.storingCharacterData
或YES
开始时, id
变为title
。因此,当元素user
结束时self.storingCharacterData
将YES
,而else if ([elementName isEqualToString:kUser])
将无效。