Question

What I'm doing: parse the XML to create NSManagedObject subclass and always getting parsing fail.

Code here:

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

where answerString is std::string. And I always getting NO in success flag.

My object class:

@interface System : NSManagedObject

@property (nonatomic, retain) NSString * systemFullName;
@property (nonatomic, retain) NSString * systemShortName;

@end

My parser header:

@class System;

@interface XMLParser : NSObject <NSXMLParserDelegate>
{
    NSMutableString *currentElementValue;

    System *system;
}

- (XMLParser *) initXMLParser;

And implementation:

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

Any ideas?

I stumbled, what when method didEndElement find end of first object, XMLParser just stops working with error 111.

Was it helpful?

Solution

I solve this problem. Instead of such a format of the XML:

<System>
    <systemFullName>aaaa</systemFullName>
    <systemShortName>a</systemShortName>
</System>
<System>
    <systemFullName>bbbb</systemFullName>
    <systemShortName>b</systemShortName>
</System>

I used this format:

<startXML>
    <System>
        <systemFullName>aaaa</systemFullName>
        <systemShortName>a</systemShortName>
    </System>
    <System>
        <systemFullName>bbbb</systemFullName>
        <systemShortName>b</systemShortName>
    </System>
</startXML>

He is the same, only with the opening and closing tags at the beginning and end of the file. Sigh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top