多个NSXMLParser获得“BAD ACCESS”

时间:2011-03-15 18:02:01

标签: iphone nsxmlparser exc-bad-access

我想用这个命令读取HTML文件:

parseHTML = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://theurl.com"]];    
[parseHTML setDelegate:self];
[parseHTML parse];
在didStartElement方法中,我希望在找到指定的字符串并使用以下信息解析新网站后停止实际的解析器:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if(somethingfound)
    {
            [parseHTML abortParsing];
            parseHTML2 = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://theurl.com" stringByAppendingString:product_link]]];
            [parseHTML2 setDelegate:self];
            [parseHTML2 parse];
    }
}

我收到了一个不正常的错误:

  

线程1:编程接收信号:“EXC   糟糕的访问“

任何想法? 问候


您好Walter感谢您的评论,这里是完整的实施: 我希望它有所帮助......: - /

  

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <ZBarReaderDelegate, NSXMLParserDelegate>{

    UIImageView *resultImage;
    UITextView *resultText;
    NSString *product_link;
    NSXMLParser *parseHTML;
    NSXMLParser *parseHTML2;

}
@property (nonatomic, retain) IBOutlet UIImageView *resultImage;
@property (nonatomic, retain) IBOutlet UITextView *resultText;
@property (nonatomic, assign) IBOutlet NSString *product_link;
@property (nonatomic, assign) NSXMLParser *parseHTML;
@property (nonatomic, assign) NSXMLParser *parseHTML2;
@property (nonatomic, retain) NSMutableArray *myMutableArray;

- (IBAction) scanButtonTapped;

@end
  

FirtViewController.m

#import "FirstViewController.h"
#import "/System/Library/Frameworks/Foundation.framework/Headers/NSDebug.h"

@implementation FirstViewController


@synthesize resultImage, resultText;
@synthesize product_link;
@synthesize parseHTML, parseHTML2;


bool link_is_here = false;
bool allergy_is_here = false; 
bool parse_one_ok = true;
int hoch = 0;


- (IBAction) scanButtonTapped
{
    NSZombieEnabled = true;

    // ADD: present a barcode reader that scans from the camera feed
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;

    ZBarImageScanner *scanner = reader.scanner;
    // TODO: (optional) additional reader configuration here

    // EXAMPLE: disable rarely used I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];

    // present and release the controller
    [self presentModalViewController: reader animated: YES];
    [reader release];
}

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{

    // ADD: get the decode results
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;

    // EXAMPLE: do something useful with the barcode data
    resultText.text = symbol.data;
    parseHTML = [[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://URL.de/suche/?q=" stringByAppendingString:symbol.data]] ]autorelease];
    [parseHTML setDelegate:self];
    [parseHTML parse];

//    [parseHTML release];

    // EXAMPLE: do something useful with the barcode image
    resultImage.image =
    [info objectForKey: UIImagePickerControllerOriginalImage];

    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    for(NSString *key in [attributeDict allKeys]) {
        if ([[attributeDict valueForKey:key] isEqualToString:@"some_string"]) {
            link_is_here = true;        
        }
        if ([key isEqualToString:@"href"] && link_is_here) {
            product_link = [attributeDict valueForKey:key];

//            [parseHTML abortParsing];

            parseHTML2 = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://URL.de" stringByAppendingString:product_link]]];
            [parseHTML2 setDelegate:self];
            [parseHTML2 parse];
            parse_one_ok = true;
            link_is_here = false;
        }
        if ([key isEqualToString:@"id"] && [[attributeDict valueForKey:key] isEqualToString:@"other_string"]) {
            allergy_is_here = true;
        }
        if ([key isEqualToString:@"title"] && allergy_is_here) {      
            NSLog(@"DO:  %@",[attributeDict valueForKey:key]);  
        }
        if ([key isEqualToString:@"id"] && [[attributeDict valueForKey:key] isEqualToString:@"string"]) {
            allergy_is_here = false;

            parse_one_ok = true;
            NSLog(@"Parser off");
            //close all parser
        }
     }
}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
    [parseHTML setDelegate:nil];
    [parseHTML2 setDelegate:nil];
    [parseHTML release];
    if (parse_one_ok) {

        [parseHTML2 release];
    }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [parseHTML release];
    [parseHTML2 release];
    self.product_link = nil;
    self.resultImage = nil;
    self.resultText = nil;
    [super dealloc];
}

@end

嘿,我的声明在标题中:

@interface FirstViewController : UIViewController <NSXMLParserDelegate>{
    NSString *product_link;
    NSXMLParser *parseHTML;
    NSXMLParser *parseHTML2;
}
@property (nonatomic, retain) IBOutlet NSString *product_link;
@property (nonatomic, retain) NSXMLParser *parseHTML;
@property (nonatomic, retain) NSXMLParser *parseHTML2;

并在来源:

@synthesize parseHTML, parseHTML2;
@synthesize product_link;

我在控制台中收到这些异常:

  

- [NSCFString setDelegate:]:发送到实例的无法识别的选择器   0x1acad0

1 个答案:

答案 0 :(得分:1)

当我必须一次解析两个文件时,我为第二个文件设置了第二个委托。委托是一个简单的NSObject,它遵循NSXML Parser Delegate协议。 (它有parserDidStartparserDidEnd,等等等等。

然后在我的第一个解析器的didEndElement中,我用这样的东西启动了第二个解析器

SoundCloudParser *scParser = [[[SoundCloudParser alloc] init]autorelease];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init]autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:scParser selector:@selector(parseXMLUrl:) object:currentArticle.uriString];
[queue addOperation:operation];
[operation release];  

我是在一个单独的线程上完成的,这就是为什么你有队列的原因。我的'parseXMLUrl:looks just like a regular one, it just sets the delegate to my second delegate object instead of setting it to自我。

您需要考虑的另一件事是您正在didStartElement工作,我发现很多时候我的解析变量中没有任何值,直到{{1} }}。这将是你需要检查的其他内容。

更新:将来,如果您尝试构建类似ZBarSDK或其他项目的东西,请说明。它会让我节省大约半个小时的代码 基本上我们需要做的是建立一个新的委托。我们称之为didEndElement。将其设置为常规NSObject并使其遵循newDelegate协议。它需要一个mutableArray或一个字典来存储它将要解析的数据,它需要一个函数来启动它。这就是它。
所以,我们假设您已在项目中创建<NSXMLDelegate>newDelegate.h,并在newDelegate.h中创建了newDelegate.mNSMutableString *currentElement; NSMutableArray *currentArticle; } - (void)parseXMLUrl:(NSString *)URL; @end     #import“Your_App_Delegate.h”     @interface newDelegate:NSObject {

didStartElement

所以,现在在你的newDelegate *ndParser = [[[newDelegateParser alloc] init]autorelease]; NSOperationQueue *queue = [[[NSOperationQueue alloc] init]autorelease]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:ndParser selector:@selector(parseXMLUrl:) object:[NSString stringWithFormat:@"http://URL.de/%@",product_link]]; [queue addOperation:operation]; [operation release]; 中你会像这样调用newDelegate:

{{1}}

这将启动第二个解析器并将其传递给解析的url。您将需要一种方法来获取该数据,因此要么将一些结果存储在App Delegate中,要么更改第二个解析器的parseXMLURL方法以返回一些值。