简单和简洁的桌面Cocoa NSXMLParser示例? [英] Simple and concise desktop Cocoa NSXMLParser example?

查看:133
本文介绍了简单和简洁的桌面Cocoa NSXMLParser示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想浏览一个文件的元素,当一个特定的元素出来,输出的标签之间的内容。



我试图遵循在Mac Dev中的示例,标题为事件驱动的XML编程,但它只是没有完成很清楚。它说,以确保我编写的代理,但它从来没有显示一个例子。我只想看一个简单的例子,其中:




  • 文件被假定为一个好的xml文件。

  • 其路径是一个URL(或字符串)。

  • 解释委托与解析器交互的方式。



Cocoa的许多教程似乎几乎教你绕过委托类并创建自己的IBAction函数,所以我错过了如何正确使用代理的培训。

解决方案

这是基于我最初为撰写的内容一个长NSString的一部分。我将来自该iOS项目的NSXMLParserDelegate代码复制到OS X项目中。它从网页中的特定对象获取文本。



.h文件:

  @interface so7576593AppDelegate:NSObject< NSApplicationDelegate,NSXMLParserDelegate> {
NSWindow * window;
IBOutlet NSTextField * textField;

NSMutableString * divCharacters;
BOOL captureCharacters;
}

@property(assign)IBOutlet NSWindow * window;

@end

.m文件:

  #importso7576593AppDelegate.h

@implementation so7576593AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
captureCharacters = NO;
NSURL * theURL = [NSURL URLWithString:@http://maxnerios.yolasite.com/];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
[parser setDelegate:self];
[parser parse];
[parser release]; (NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName属性(NSString *)* * * * * * * * * * * * * * * * * * * :(NSDictionary *)attributeDict {
if([elementName isEqual:@div]&& [attributeDict objectForKey:@id] isEqual:@I3_sys_txt]){
captureCharacters = YES;
divCharacters = [[NSMutableString alloc] initWithCapacity:500];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(captureCharacters){
// from parser:foundCharacters:docs:
//解析器对象可以发送委托几个解析器:foundCharacters:messages来报告一个元素的字符。
//因为string可能只是当前元素的总字符内容的一部分,所以应该将它追加到当前
//字符的累积,直到元素更改。
[divCharacters appendString:string]; (NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName(NSString *)。 {
if(captureCharacters){
captureCharacters = NO;
[textField setStringValue:divCharacters];
[divCharacters release];
}
}

@end


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.

解决方案

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:

@interface so7576593AppDelegate : NSObject <NSApplicationDelegate, NSXMLParserDelegate> {
    NSWindow *window;
    IBOutlet NSTextField *textField;

    NSMutableString *divCharacters;
    BOOL captureCharacters; 
}

@property (assign) IBOutlet NSWindow *window;

@end

.m file:

#import "so7576593AppDelegate.h"

@implementation so7576593AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    captureCharacters = NO;
    NSURL *theURL = [NSURL URLWithString:@"http://maxnerios.yolasite.com/"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
    [parser setDelegate:self];
    [parser parse];
    [parser release];

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqual:@"div"] && [[attributeDict objectForKey:@"id"] isEqual:@"I3_sys_txt"]) {
        captureCharacters = YES;
        divCharacters = [[NSMutableString alloc] initWithCapacity:500];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (captureCharacters) {
        //from parser:foundCharacters: docs:
        //The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. 
        //Because string may be only part of the total character content for the current element, you should append it to the current 
        //accumulation of characters until the element changes.
        [divCharacters appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if (captureCharacters) {
        captureCharacters = NO;
        [textField setStringValue:divCharacters];
        [divCharacters release];
    }
}

@end 

这篇关于简单和简洁的桌面Cocoa NSXMLParser示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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