I want to check a data version number in my XML and if the XML's version number is newer then the user's current version (saved in NSUserDefaults), the XML is parsed and inserted into the SQLite store, otherwise parsing is stopped and nothing happens. I've got the parsing and storing part figured out, but I can't figure out how to stop the XML parsing once started.
My XML Looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<DBName.sqlite>
<data version="1">
<rows>
<row>
<someInt>0</someInt>
<someString>Howdy</someString>
<rowVersion>0</rowVersion>
</row>
...
</rows>
</data>
</DBName.sqlite>
So what I'm trying to do is to stop the NSXMLParser if the user's previously stored dataversionCode == the XML dataversionCode.
My parsing method looks like this:
- (BOOL)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error {
BOOL result = YES;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:nil];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
开发者_C百科 NSError *parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
result = NO;
}
[parser release];
return result;
}
Any help is appreciated. lq
I found a solution, thanks to Rob Napier's nudge:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if (qName) {
elementName = qName;
}
if ([elementName isEqualToString:@"data"]) {
int dataVersion = [[attributeDict objectForKey:@"version"] intValue];
// Match dataVersion with whatever desired value
if (dataVersion <= someValue) {
[parser abortParsing];
}
}
. . .
}
Implement a delegate (which you'll need anyway). In the delegate, when you parse the version if it is not what you want, then call [parser abortParsing]
.
精彩评论