Phil
Phil

Reputation: 461

Multiple NSXMLParser get "BAD ACCESS"

i want to read a HTML File with this command:

parseHTML = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:[@"http://theurl.com"]];    
[parseHTML setDelegate:self];
[parseHTML parse];

in the didStartElement method i want to stop the actual parser after he found a specified string and parse a new site with these information:

- (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];
    }
}

i get an irregular error:

Thread1: Program received signal: "EXC BAD ACCESS"

any ideas? regards


Hello Walter thanks for your comment, here is the complete implementation: I hope it helps... :-/

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

hey my declaration is in the header:

@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;

and in the source:

@synthesize parseHTML, parseHTML2;
@synthesize product_link;

i get these exception in console:

-[NSCFString setDelegate:]: unrecognized selector sent to instance 0x1acad0

Upvotes: 0

Views: 662

Answers (1)

Walter
Walter

Reputation: 5887

When I have to parse two files at once I set up a second delegate for the second file. The delegate is a simple NSObject that adheres to the NSXML Parser Delegate protocol. (It has the parserDidStart, parserDidEnd, blah blah blah).

Then in my didEndElement of my first parser I kick off the second parser with something like this

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];  

I do it on a separate thread which is why you have the queue in there. My 'parseXMLUrl:looks just like a regular one, it just sets the delegate to my second delegate object instead of setting it toself`.

One other thing you need to think about is that you are doing work at the didStartElement and I find that a lot of times I don't have any values in my parsing variables until I get to didEndElement. That would be something else for you to check.

UPDATE: In the future, if you are trying to build on something like ZBarSDK or another project, please say so. It would have saved me about half an hour of messing around with your code.
Basically what we need to do is to set up a new delegate. Let's call it newDelegate. Set it up as a regular NSObject and make it follow <NSXMLDelegate> protocol. It needs a mutableArray or a dictionary to store the data it will parse and it needs a function to kick it off. And that's about it.
So, let's assume you have created newDelegate.h and newDelegate.m in your project and in newDelegate.h you have #import "Your_App_Delegate.h" @interface newDelegate : NSObject {

NSMutableString *currentElement;
NSMutableArray *currentArticle;
}

- (void)parseXMLUrl:(NSString *)URL;
@end

So, now in your didStartElement you would call the newDelegate like this:

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];    

This would kick off the second parser and pass it the url to parse. You will need a way to get that data, so either store some results in your App Delegate or else change the parseXMLURL method of the second parser to return some value.

Upvotes: 1

Related Questions