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

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

问题描述

今天我正在研究如何在Cocoa(桌面)中创建一个简单的XML解析器。我想使用NSXMLParser来解析数据,但不知道从哪里开始。 Web上的XML文件没有太多的数据,只是一个简单的列表,有一些我需要保存到变量的东西。任何人都有任何建议,如何做到这一点,因为关于这个在线文档没有太多的意义。



感谢任何帮助!



EDIT 我想要创建XML解析器的原因是从服务器上的MYSQL数据库获取信息到客户端应用程序。如果有一个更好的方法来做这个除了XML解析器,请让我知道!

解决方案

这是它的工作原理: / p>

有一个类NSXMLParser。它用于解析XML文件。但是,NSXMLParser是愚蠢的。它知道如何做是解析XML,但它不知道它应该对它找到的信息做什么。



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



所以你可能有类似this:

  NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:someURLToAnXMLFile]; 
[parser setDelegate:self];
[parser parse];
[parser release];然后在同一个对象(self)中,你可能有一些这些方法:

   - (void)parserDidStartDocument:(NSXMLParser *)parser {
//解析器启动此文档。你会怎样做? (NSString *)namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName属性:(NSDictionary *)* * * * * * * * * * * * * * * * * )attributeDict {
//解析器找到一个XML标签并给你一些信息
//你要做什么?
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//解析器在开始和结束标记之间找到一些字符
//你要做什么?
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
//解析器完成。你会怎样做?
}

文档中列出了一大堆这些方法。只需转到NSXMLParser类引用,它们都列在委托方法部分下。一旦你得到它的悬念,NSXMLParser是很容易使用。这是一个 SAX解析器,这意味着它是事件驱动的解析器。它找到的东西,它告诉你这件事。


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.

Thanks for any help!

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!

解决方案

Here's how it works:

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.

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];

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?
}

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天全站免登陆