0

我是 iOS 编程的新手,正在尝试构建一个与另一台服务器通信以检索 XML 并解析它的小应用程序。现在我已经在同一个视图控制器中实现了 NSXML 解析器委托,我想编写一个单独的类来接受 XML 路径,解析它并返回有意义的数据。

所以在视图控制器中我有这个

// On Foo Button Click
XMLParser* xmlParser = [[XMLParser alloc] init];
// XMLParser would be my class file which has a method to parse XML
xmlParser.readXMLFromPath(pathToXML);

在 XMLParser 类文件中:

- (NSMutableDictionary*) readXMLFromPath: (NSString* pathToXML) {
  NSMutableDictionary* resultSet;
  // Alloc NSXMLParser and implement its delegate methods
  // resultSet would now hold the needed Data
}

那么现在我应该如何返回resultSetviewController 呢?

4

2 回答 2

0

让 XML 解析器的客户端传入一个块,以便在结果准备好时执行,而不是立即返回它们。您还可以让客户端在出现问题时传入单独的 errorBlock。

- (void)readXMLFromPathWithSuccessBlock:(void (^)(NSMutableDictionary *results))successBlock;
 {
  // Alloc NSXMLParser and implement its delegate methods
  // Save successBlock in a property
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
  // Call success block and pass results to the client
}
于 2013-10-08T17:23:29.033 回答
0

这是 NSOperation 的一个子类,我用它来对 NSOperationQueue 进行所有 xml 解析:

#import <Foundation/Foundation.h>
#import "GDBaseParser.h"

@class ParseXMLOperation;

@protocol ParseXMLCallbackDelegate <NSObject>

-(void)parseOperationComplete:(ParseXMLOperation *)completedOperation;

@end

@interface ParseXMLOperation : NSOperation 
{
    GDBaseParser *xmlParserDelegate;
    NSData *dataToParse;
    id <ParseXMLCallbackDelegate> callbackDelegate;
}

@property(nonatomic,strong)GDBaseParser *xmlParserDelegate;
@property(nonatomic,strong)NSData *dataToParse;
@property(nonatomic,strong)id <ParseXMLCallbackDelegate> callbackDelegate;
-(id)initWithParser:(GDBaseParser *)inParserDelegate;
@end


@implementation ParseXMLOperation

@synthesize xmlParserDelegate, dataToParse, callbackDelegate;

-(id)initWithParser:(GDBaseParser *)inParserDelegate
{
    self = [super init];
    if( self != nil )
    {
        self.xmlParserDelegate = inParserDelegate;
    }
    return self;
}

-(void)main
{
    @autoreleasepool
    {

        NSXMLParser *parser  = [[NSXMLParser alloc] initWithData:dataToParse];
        xmlParserDelegate.currentContext = localContext;
        [parser setDelegate:xmlParserDelegate];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
        [self performSelectorOnMainThread:@selector(sendOperationComplete) withObject:nil waitUntilDone:NO];

    }


}


-(void)sendOperationComplete
{
    if( callbackDelegate != nil && [callbackDelegate     respondsToSelector:@selector(parseOperationComplete:)] )
    {
        [callbackDelegate parseOperationComplete:self];
    }
}

然后我可以设置操作并将其添加到队列中:

         MyParser *parserDelegate = [[MyParser alloc] init];
ParseXMLOperation *parseOperation = [[ParseXMLOperation alloc]   initWithParser:parserDelegate];
         parseOperation.dataToParse = parseData;
         parseOperation.completionBlock = ^{
             localOperation.messageHeaderDict = parserDelegate.messageHeaders;
             dispatch_async(dispatch_get_main_queue(), ^{
                 //notify your UI that you are done with your parsing
             });

         };
         [self.xmlParsingQueue addOperation:parseOperation];
于 2013-10-08T17:23:44.897 回答