Import a 100 MB XML file into the iOS basic data model

In my application, I need to import a 100 MB file into the underlying data model.

So far, I have imported a 100K xml file and everything is working fine. However, I’m not sure how long it will take to import 100 MB xml. I will launch it with tonite, while ... do you think this approach is good?

thank

NSManagedObjectContext * context = [self managedObjectContext];

    // Delete all documents
    NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
    [fetch setEntity:[NSEntityDescription entityForName:@"Document" inManagedObjectContext:context]];
    NSArray * result = [context executeFetchRequest:fetch error:nil];
    for (id basket in result)
        [context deleteObject:basket];



    //Insert documents
    TBXML * tbxml = [[TBXML tbxmlWithXMLFile:@"categ_small.xml"] retain];
    TBXMLElement * root = tbxml.rootXMLElement;
    TBXMLElement * doc = [TBXML childElementNamed:@"doc" parentElement:root];

    do {
        TBXMLElement * idDoc = [TBXML childElementNamed:@"id" parentElement:doc];
        TBXMLElement * titleDoc = [TBXML childElementNamed:@"title" parentElement:doc];
        TBXMLElement * descriptionDoc = [TBXML childElementNamed:@"description" parentElement:doc];
        TBXMLElement * time = [TBXML childElementNamed:@"time" parentElement:doc];
        TBXMLElement * tags = [TBXML childElementNamed:@"tags" parentElement:doc];
        TBXMLElement * geo = [TBXML childElementNamed:@"geo" parentElement:doc];
        TBXMLElement * event = [TBXML childElementNamed:@"event" parentElement:doc];
        TBXMLElement * user = [TBXML childElementNamed:@"user" parentElement:doc];
        TBXMLElement * categ = [TBXML childElementNamed:@"categ" parentElement:doc];

        NSManagedObject *newDocument = [NSEntityDescription
                                        insertNewObjectForEntityForName:@"Document"
                                        inManagedObjectContext:context];

        [newDocument setValue:[TBXML textForElement:idDoc] forKey:@"idDoc"];
        [newDocument setValue:[TBXML textForElement:titleDoc] forKey:@"titleDoc"];
        [newDocument setValue:[TBXML textForElement:descriptionDoc] forKey:@"descriptionDoc"];
        [newDocument setValue:[TBXML textForElement:time] forKey:@"time"];
        [newDocument setValue:[TBXML textForElement:tags] forKey:@"tags"];
        [newDocument setValue:[TBXML textForElement:geo] forKey:@"geo"];
        [newDocument setValue:[TBXML textForElement:event] forKey:@"event"];
        [newDocument setValue:[TBXML textForElement:user] forKey:@"user"];
        [newDocument setValue:[TBXML textForElement:categ] forKey:@"categ"];

    } while ((doc = doc->nextSibling));  

UPDATE This is a one-time operation that is performed only in the simulator and it will not be deployed with the final application.

+3
source share
3 answers

I would take the following steps:

  • XML- > SQLite ruby ​​ php, , SQLite Manager, Firefox SQLite XML.

  • : /

+4

, , .

XML-, , , . . Apple NSXMLParser , , . ( - .)

NSOperation, . .

NSEntityDescription, XML , . .

+3

If this is a one-time process used to create a CoreData or SQLite database for distribution with your application, this will most likely work, but only on a simulator. However, if you need to do this on a device, you need to switch to a streaming parser.

0
source

All Articles