0
我做了這個XML解析器:如何保存核心數據與XML解析器
XMLToObjectParser.m:
#import "XMLToObjectParser.h"
@implementation XMLToObjectParser
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (NSArray *)items
{
return items;
}
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error
{
//[items release];
items = [[NSMutableArray alloc] init];
className = aClassName;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
if([parser parserError] && error) {
*error = [parser parserError];
}
//[parser release];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:className]) {
// create an instance of a class on run-time
item = [NSEntityDescription entityForName:className inManagedObjectContext:managedObjectContext];
}
else {
currentNodeName = [elementName copy];
currentNodeContent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
//NSLog(@"Close tag: %@", elementName);
if([elementName isEqualToString:className]) {
[items addObject:item];
//[item release];
item = nil;
}
else if([elementName isEqualToString:currentNodeName]) {
// use key-value coding
[item setValue:currentNodeContent forKey:elementName];
//[currentNodeContent release];
currentNodeContent = nil;
//[currentNodeName release];
currentNodeName = nil;
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
[currentNodeContent appendString:string];
}
- (void)dealloc
{
//[items release];
//[super dealloc];
}
@end
XMLToObjectParser.h:
#import <Foundation/Foundation.h>
@interface XMLToObjectParser : NSObject <NSXMLParserDelegate> {
NSString *className;
NSMutableArray *items;
NSObject *item; // stands for any class
NSString *currentNodeName;
NSMutableString *currentNodeContent;
NSManagedObjectContext *managedObjectContext;
}
- (NSArray *)items;
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error;
@end
它解析像這樣的XML :
<section id="2">
<label>Animaux</label>
<image>Images/Rayons/Bandeau/Animaux.png</image>
<key>Images/Rayons/Bandeau/Animaux.png</key>
<products>
<Product id="21">
<category_id>Chat</category_id>
<label>Aliments pour chat</label>
<price>2.00</price>
</Product>
<Product id="1286">
<category_id>Chat</category_id>
<label>Boite de paté</label>
<price>0.00</price>
</Product>
</products>
</sections>
但它在t上崩潰他行
item = [NSEntityDescription entityForName:className inManagedObjectContext:managedObjectContext];
任何人都可以幫我嗎?
謝謝
是的,但如何從我的xmlParser類獲取ctx?對不起,但我從目標C開始。 – Sebastien 2012-02-03 18:26:16
你的代碼在哪裏分配了你的'NSManagedObjectContext'? – mvds 2012-02-03 18:30:32
我把它放在我的Parser.h中,事實是我創建了一個沒有核心數據的項目,我可能忘記了一些東西。 – Sebastien 2012-02-03 18:41:56