在 Cocoa 中解析 XML [英] Parsing XML in Cocoa

查看:34
本文介绍了在 Cocoa 中解析 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我正在研究如何在 Cocoa 中制作一个简单的 XML 解析器(用于桌面).我正在考虑使用 NSXMLParser 来解析数据,但我不太确定从哪里开始.Web 上的 XML 文件中没有太多数据,只是一个简单的列表,其中包含一些我需要保存到变量中的内容.有没有人对如何执行此操作有任何建议,因为关于此的在线文档没有太大意义.

Today I am looking into how to make a simple XML parser in Cocoa (for the desktop). I am thinking of using NSXMLParser to parse the data, but am not quite sure where to start. The XML file on the web doesn't have the much data in it, just a simple listing with a few things that I need to save into a variable. Does anyone have any suggestions on how to do this, as the online documentation about this isn't making too much sense.

感谢您的帮助!

EDIT 我想要创建 XML 解析器的原因是为了从服务器上的 MYSQL 数据库获取信息到客户端应用程序.如果除了 XML 解析器之外还有更好的方法可以做到这一点,请告诉我!

EDIT The reason why I am wanting to create an XML parser is to get information from a MYSQL database on a server to the client application. If there is some better way to do this besides a XML parser, please let me know!

推荐答案

这是它的工作原理:

有一个类叫做 NSXMLParser.它用于解析 XML 文件.然而, NSXMLParser 是愚蠢的.它只知道如何解析 XML,但不知道它应该如何处理它找到的信息.

There's a class called NSXMLParser. It's used to parse XML files. However, NSXMLParser is stupid. All it knows how to do is parse XML, but it doesn't know what it's supposed to do with the information it finds.

输入代表.一个代表就像一个保姆.由于 XMLParser 不知道如何处理它找到的信息,它会就每一件事询问它的代表:嘿!我开始解析一个文档!我应该做些什么吗?"嘿!我找到了一些 CDATA!我该怎么处理它!"嘿!我发现了另一个标签!"嘿!我发现了一个结束标签!",等等.所有这些嘿!"语句是委托方法,或者换句话说,它们是委托对象可以选择实现的可选方法.通常(但不总是),创建 NSXMLParser 的对象也是委托,但不一定是这样.

Enter a delegate. A delegate is like a nanny. Since the XMLParser doesn't have a clue what to do with the information it finds, it goes and asks its delegate about each and every thing: "Hey! I started parsing a document! Am I supposed to do anything?" "Hey! I found some CDATA! What am I supposed to do with it!" "Hey! I found another tag!" "Hey! I found a closing tag!", and so on. All of these "Hey!" statements are delegate methods, or in other words, they are optional methods that a delegate object may choose to implement. Usually (but not always), the object that creates the NSXMLParser is also the delegate, but that doesn't have to be the case.

所以你可能有这样的事情:

So you might have something like this:

NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:someURLToAnXMLFile];
[parser setDelegate:self];
[parser parse];
[parser release];

然后在同一个对象(self)中,你可能有以下一些方法:

Then in that same object (self), you might have some of these methods:

- (void)parserDidStartDocument:(NSXMLParser *)parser {
  //the parser started this document. what are you going to do?
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
  //the parser found an XML tag and is giving you some information about it
  //what are you going to do?
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  //the parser found some characters inbetween an opening and closing tag
  //what are you going to do?
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
  //the parser finished. what are you going to do?
}

文档中列出了一大堆这些方法.只需转到 NSXMLParser 类参考,它们都列在委托方法"部分下.一旦掌握了窍门,NSXMLParser 就非常容易使用.它是一个 SAX 解析器,这意味着它是事件驱动的解析器.它会找到东西,然后告诉你.

There are a whole bunch of these methods listed in the documentation. Simply go to the NSXMLParser class reference, and they're all listed under the "Delegate Methods" section. Once you get the hang of it, NSXMLParser is pretty easy to use. It is a SAX Parser, which means it's event-driven parser. It finds stuff, and it tells you about it.

这篇关于在 Cocoa 中解析 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆