NSXMLParser解析器:解析xml时未触发didStartElement

发布于 2024-12-29 05:07:49 字数 1243 浏览 2 评论 0原文

我正在尝试解析 xml 文件并且没有收到错误,但是在尝试读取它时,解析器:didStartElement 事件未触发。我做错了什么?感谢您的帮助。

- (void)viewDidLoad
        {
            [super viewDidLoad];

            // xml connect
            NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/list.xml"];
            NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

            //Start parsing the XML file.
            BOOL success = [xmlParser parse];

            if(success)
                NSLog(@"No Errors");
            else
                NSLog(@"Error!");
        }

        // reading xml...

        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
          namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
            attributes:(NSDictionary *)attributeDict {

            if([elementName isEqualToString:@"main"]) {
                //Initialize the array.
                apps = [[NSMutableArray alloc] init];
            }
            else if([elementName isEqualToString:@"prog"]) {

                //Extract the attribute here.
                idUsuari = [attributeDict objectForKey:@"Id"];

                NSLog(@"ID: %@", idUser);
            }
        }

I am trying to parse xml file and get no errors but when trying to read it, parser:didStartElement event is not triggered. What I am doing wrong? Thanks for help.

- (void)viewDidLoad
        {
            [super viewDidLoad];

            // xml connect
            NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/list.xml"];
            NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

            //Start parsing the XML file.
            BOOL success = [xmlParser parse];

            if(success)
                NSLog(@"No Errors");
            else
                NSLog(@"Error!");
        }

        // reading xml...

        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
          namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
            attributes:(NSDictionary *)attributeDict {

            if([elementName isEqualToString:@"main"]) {
                //Initialize the array.
                apps = [[NSMutableArray alloc] init];
            }
            else if([elementName isEqualToString:@"prog"]) {

                //Extract the attribute here.
                idUsuari = [attributeDict objectForKey:@"Id"];

                NSLog(@"ID: %@", idUser);
            }
        }

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

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

发布评论

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

评论(3

长伴 2025-01-05 05:07:49

您尚未将视图控制器设置为 XML 解析器的委托:

[xmlParser setDelegate:self];

在分配/初始化之后。如果没有这个,解析器就不知道将委托消息发送给谁。

You haven't set your view controller as the XML Parser's delegate:

[xmlParser setDelegate:self];

After you alloc / init. Without this the parser does not know who to send the delegate messages to.

小矜持 2025-01-05 05:07:49

然后在解析之前声明您当前的类符合 NSXMLParserDelegate

[xmlParser setDelegate:self];

以便解析器知道可以调用您实现的委托方法。

例如

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict

declare that your current class conforms to NSXMLParserDelegate, then

[xmlParser setDelegate:self];

before you parse, so the parser knows can call the delegate methods implemented by you.

eg

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict
深空失忆 2025-01-05 05:07:49

再次运行后请将此添加到 Info.plist

Please add this to Info.plist after run again

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