我正在做的是:解析XML以创建NSManagedObject子类,但总是解析失败。
此处代码:
NSData* data = [[NSString stringWithUTF8String:answerString.c_str()] dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[nsXmlParser setDelegate:parser];
BOOL success = [nsXmlParser parse];
哪里
answerString
是
std::string
。我总是
NO
在里面
success
旗帜
我的对象类:
@interface System : NSManagedObject
@property (nonatomic, retain) NSString * systemFullName;
@property (nonatomic, retain) NSString * systemShortName;
@end
我的解析器标头:
@class System;
@interface XMLParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentElementValue;
System *system;
}
- (XMLParser *) initXMLParser;
和实施:
- (XMLParser *) initXMLParser
{
self = [super init];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"System"])
{
system = [NSEntityDescription insertNewObjectForEntityForName:@"System"
inManagedObjectContext:((AppDelegate*)[[UIApplication sharedApplication]
delegate]).managedObjectContext];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else
{
[currentElementValue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication]
delegate]).managedObjectContext;
if ([elementName isEqualToString:@"System"])
{
[context save:nil];
system = nil;
}
if ([elementName isEqualToString:@"systemFullName"])
{
system.systemFullName = currentElementValue;
}
if ([elementName isEqualToString:@"systemShortName"])
{
system.systemShortName = currentElementValue;
}
}
有什么想法吗?
我跌跌撞撞,什么时候方法
didEndElement
找到第一个对象的结尾,XMLParser就停止工作,出现错误111。