无法使用 NSXML 解析器在 NSMutableArray 中存储数据
我想使用 NSXml 解析器从 Web 服务获取数据。我能够接收数据(我可以使用 NSLog 看到这一点。但我无法将数据保存在数组中。这是我的代码。
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqual:@"entry"]) {
NSLog(@"Found a title entry");
getXMLFlag = YES;
}
if ([elementName isEqual:@"title"] && getXMLFlag) {
NSLog(@"found title!");
PTString = [[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
namazXmlArray= [[NSMutableArray alloc] initWithObjects:@"tab1",@"1tab1",nil];
if ([elementName isEqual:@"title"] && getXMLFlag) {
NSLog(@"ended title: %@",PTString);
// namazXmlArray= [[NSMutableArray alloc] initWithObjects:@"tab1",@"1tab1",nil];
[namazXmlArray addObject:PTString];
NSLog(@"I can't add this in array: %@",PTString);
//Release and nil title String so that the next time characters are found and not within a title tag, they are ignored
[PTString release];
PTString = nil;
}
if ([elementName isEqual:@"entry"]) {
NSLog(@"ended a namaz entry");
getXMLFlag = NO;
}
}
I want to get data from webservice using NSXml Parser. I am able to receive the data(i can see that using NSLog. but i couldn't save the data in an array. this is my code.
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqual:@"entry"]) {
NSLog(@"Found a title entry");
getXMLFlag = YES;
}
if ([elementName isEqual:@"title"] && getXMLFlag) {
NSLog(@"found title!");
PTString = [[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
namazXmlArray= [[NSMutableArray alloc] initWithObjects:@"tab1",@"1tab1",nil];
if ([elementName isEqual:@"title"] && getXMLFlag) {
NSLog(@"ended title: %@",PTString);
// namazXmlArray= [[NSMutableArray alloc] initWithObjects:@"tab1",@"1tab1",nil];
[namazXmlArray addObject:PTString];
NSLog(@"I can't add this in array: %@",PTString);
//Release and nil title String so that the next time characters are found and not within a title tag, they are ignored
[PTString release];
PTString = nil;
}
if ([elementName isEqual:@"entry"]) {
NSLog(@"ended a namaz entry");
getXMLFlag = NO;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在将一个数组添加到另一个数组。我不太清楚为什么,如果没有完整的代码,很难看到你在做什么。这并没有具体回答您的问题,但作为 iPhone 编程的新手,您可能想看看我为 XML 解析提供的类。然后,您将能够仅使用几行代码来解析该文件。您可以在 GitHub 上获取它。
PS 发帖时请将所有代码放在代码括号内。它更容易阅读:)
更新
将数据放入数组后,您需要执行以下操作:
You seem to be adding one array to another array. I'm not too sure why, and it's difficult to see what you are doing without your full code. This doesn't specifically answer your question but as your new to iPhone programming you might want to look at the class I have made available for XML parsing. You will then be able to parse the file using only a few lines of code. You can get it on GitHub here.
P.S Put all the code in code brackets when you post. It's easier to read :)
UPDATE
Once you have the data in the array you need to do the following: