Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Error parsig XML with NSXMLParser::abortParsing

Hi there:

I'm dealing with a problem I can't fix when parsing XML files.
I've downloaded SeismicXML sample application an followed the rules to parse xml files in my program, but it seems I have a bug I cannot find.
Parsing of documents works fine when the number of elements parsed is lower than the maximun threshold I set, but when it's higher and abortParsing method is invoked, I always get a parser internal error (NSXMLParserErrorDomain: error 1).

I copy my code for more detail:

#import "Track.h"

@interface TracksXMLParser : NSObject {

NSMutableString *currentContent;
Track *currentTrack;
NSMutableArray *tracks;

}

- (void) parseTracksFromAlbumId: (NSString *) albumId;

@end


#import "TracksXMLParser.h"
#import "Track.h"
#import "GlobalConstants.h"

static NSUInteger parsedTracksCount;

@implementation TracksXMLParser


// Limit the number of parsed artists to 35. Otherwise the application runs very slowly on the device.
#define MAX_TRACKS 35


- (void) parseTracks: (NSString *) url {

// Show activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// Build search URL
NSURL *searchURL = [[NSURL alloc] initWithString: url];

// Define the parser
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL: searchURL];

// Th¡s class will handle the parse callbacks
[parser setDelegate: self];

// Parser tunning
[parser setShouldProcessNamespaces: NO];
[parser setShouldReportNamespacePrefixes: NO];
[parser setShouldResolveExternalEntities: NO];

// Process parsing
[parser parse];

// Catch errors
NSError *parserError = [parser parserError];
if (parserError) {
// Handle error
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"ERROR: Could not parse document -> %@", [parserError localizedDescription]);
}

[searchString release];
[searchURL release];
[parser release];
}

- (void) parserDidStartDocument: (NSXMLParser *) parser {
// Initialization
tracks = [[NSMutableArray alloc] init];
parsedTracksCount = 0;
}


- (void) parser: (NSXMLParser *) parser didStartElement: (NSString *) elementName namespaceURI: (NSString *) namespaceURI
qualifiedName: (NSString *) qName attributes: (NSDictionary *) attributeDict {

if (qName) elementName = qName;

if ([elementName isEqualToString: @"track"]) {
// Check the elements count threshlod
if (parsedTracksCount >= MAX_TRACKS) {
[parser abortParsing];
}
parsedTracksCount++;
// Create a new Track instance
currentTrack = [[Track alloc] init];
// Get the album tag properties
currentTrack.identifier = [attributeDict valueForKey: @"id"];
return;
}

if ([elementName isEqualToString: @"title"] || [elementName isEqualToString: @"duration"]) {
// Create a mutable string to hold the contents of the 'updated' element.
// The contents are collected in parser:foundCharacters:.
currentContent = [NSMutableString string];
} else {
// The element isn't one that we care about, so set the property that holds the
// character content of the current element to nil. That way, in the parser:foundCharacters:
// callback, the string that the parser reports will be ignored.
currentContent = nil;
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentContent) {
// If the current element is one whose content we care about, append 'string'
// to the property that holds the content of the current element.
[currentContent appendString:string];
}
}


- (void) parser: (NSXMLParser *) parser didEndElement: (NSString *) elementName namespaceURI: (NSString *) namespaceURI
qualifiedName: (NSString *) qName
{
if (qName) elementName = qName;

if ([elementName isEqualToString:@"title"]) {
currentTrack.title = currentContent;
} else if ([elementName isEqualToString: @"duration"]) {
currentTrack.duration = (NSInteger *) [currentContent intValue];
} else if ([elementName isEqualToString: @"track"]) {
[tracks addObject: currentTrack];
}
}

- (void) parserDidEndDocument:(NSXMLParser *) parser {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[[NSNotificationCenter defaultCenter] postNotificationName: [GlobalConstants NTF TRACKSPARSED] object: tracks];
}

- (void) dealloc
{
[currentContent release];
[currentTrack release];
[tracks release];
[super dealloc];
}


Any suggestions?
Thanks in advance!!

iMac 24", Mac OS X (10.5.5)

Posted on Sep 25, 2008 1:44 AM

Reply

There are no replies.

Error parsig XML with NSXMLParser::abortParsing

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.