无法使用 NSXML 解析器在 NSMutableArray 中存储数据

发布于 2024-10-30 02:31:59 字数 1426 浏览 2 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(1

梦里寻她 2024-11-06 02:31:59

您似乎正在将一个数组添加到另一个数组。我不太清楚为什么,如果没有完整的代码,很难看到你在做什么。这并没有具体回答您的问题,但作为 iPhone 编程的新手,您可能想看看我为 XML 解析提供的类。然后,您将能够仅使用几行代码来解析该文件。您可以在 GitHub 上获取它。

PS 发帖时请将所有代码放在代码括号内。它更容易阅读:)

更新
将数据放入数组后,您需要执行以下操作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      ....
         cell.textLabel.text = [[array objectAtIndex:storyIndex] objectForKey:@"title"];

}

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:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      ....
         cell.textLabel.text = [[array objectAtIndex:storyIndex] objectForKey:@"title"];

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