简单简洁的桌面Cocoa NSXMLParser例子?
我想查看文件的元素,当出现一个特定元素时,输出标签之间的内容。
我尝试遵循 Mac Dev 中标题为 事件驱动的 XML 编程,但它只是没有完成得很清楚。它说要确保我对代表进行编码,但它从未显示示例。我只想看一个简单的示例,其中:
- 该文件被假定为一个好的 xml 文件。
- 它的路径是一个 URL(或字符串)。
- 解释了委托与解析器交互的方式。
Cocoa 的许多教程似乎几乎都是教您绕过委托类并创建自己的 IBAction 函数,因此我错过了我认为如何正确使用委托的培训。在示例中不清楚我是否应该在委托类中构建委托,或者将它们保留在带有解析器的类中。
I would like to look through the elements of a file and when one specific element comes out, output the contents in between the tag.
I tried to follow the example in the Mac Dev entitled Event Driven XML Programming, but it just doesn't finish very clearly. It says to make sure I code the delegates, but it never shows an example. I just want to see a simple example where:
- The file is assumed to be a good xml file.
- Its path is a URL (or string).
- The way the delegate interacts with the parser is explained.
Many tutorials for Cocoa seem to almost teach you to circumvent the delegate classes and make your own IBAction functions so I'm missing the training I think on how to use the delegates properly. Its not clear in the example if I'm supposed to build the delegates in the delegate class or keep them in the class with the parser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是基于我最初为 剪掉长的一部分NSString。我将 NSXMLParserDelegate 代码从该 iOS 项目复制到 OS X 项目中。它从网页中的特定对象获取文本。
.h 文件:
.m 文件:
This is based on something I originally wrote for Cut out a part of a long NSString. I copied the NSXMLParserDelegate code from that iOS project into an OS X project. It gets the text from a specific object in a web page.
.h file:
.m file:
如果单击该页面上的“下一步”链接并转到“处理 XML 元素和属性”,它将为您提供如何对委托进行编码的示例。
Apple 在 ImageMap 中提供了 Mac 示例。
Mac 和 iPhone 上的
NSXMLParser
之间没有区别,因此阅读 iPhone 示例应该不成问题。If you click the "Next" link on that page and go onto "Handling XML Elements and Attributes" it will give you an example of how to code the delegates.
Apple provides a Mac example in ImageMap.
There's no difference between
NSXMLParser
on Mac and iPhone, so reading an iPhone example shouldn't be a problem.下面是在自定义类中使用
NSXMLParser
的示例,该类接收要查找的标签字符串和 xml NSData:JHXMLParser.h:
和 JHXMLParser.m:
如下使用:
然后实现委托方法:
Here is an example of using an
NSXMLParser
in a custom class that takes in a string of the tag to look for and the xml NSData:JHXMLParser.h:
And the JHXMLParser.m:
Used like so:
Then implement the delegate method: