且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

简单和简洁的桌面Cocoa NSXMLParser示例?

更新时间:2022-10-15 23:21:36

。在示例中不清楚,如果我要在委托类中构建委托, >

这是基于我最初为撰写的内容一个长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