简体   繁体   English

iPhone编码:从XML文件接收数据并要保存到SQLite

[英]IPhone coding: receiving data from XML file and want save to SQLite

i am trying to setup a iPhone app that pulls XML file from the internet and then want it to save to a SQLite database. 我正在尝试设置一个iPhone应用程序,该应用程序从互联网上提取XML文件,然后希望将其保存到SQLite数据库中。 the coding i am using to parser the XML file is: 我用来解析XML文件的编码是:

SQLViewAppDelegate.m SQLViewAppDelegate.m

NSURL *url = [[NSURL alloc] initWithString:@"http://www.180designlab.com/waxjambu/getupdates.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
NSLog(@"Info array has %d items", [infoGet count]);
totalCount = [infoGet count];
//[self save_Clicked];

if(success)
  NSLog(@"No Errors");
else 
  NSLog(@"Error Error Error");

XMLParser.m XMLParser.m

-(XMLParser *)initXMLParser
{

[super init];
appDelegate = (SQLViewAppDelegate *)[[UIApplication sharedApplication] delegate];

return self;
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{


if ([elementName isEqualToString:@"Updates"]) 
{


appDelegate.infoGet = [[NSMutableArray alloc] init];


}


else if ([elementName isEqualToString:@"Update"])
{
            aInfo = [[XMLInfo alloc] init];
    aInfo.infoGetID = [[attributeDict objectForKey:@"id"] integerValue];

    //aGetInfo.info
    NSLog(@"Info ID Value: %i", aInfo.infoGetID);
}



NSLog(@"Processing Element: %@", elementName);

} }

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

 {



if(!currentElementValue)
    currentElementValue = [[NSMutableString alloc] initWithString:string];
  else 
    [currentElementValue appendString:string];
    //[self saveData];
    NSLog(@"Processing Value: %@", currentElementValue);
    NSLog(@"Processing ID: %d", aInfo.infoGetID);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

 {

if ([elementName isEqualToString:@"Updates"])
    return;
    if ([elementName isEqualToString:@"Update"]) 

{


[appDelegate.infoGet addObject:aInfo];

        [aInfo release];
        aInfo = nil;


}

else
    [aInfo setValue:currentElementValue forKey:elementName];
    [currentElementValue release];
    currentElementValue = nil;

}

I can get the data in to a tableView but can not work out how to save them to SQLite file I have up. 我可以将数据放入tableView中,但无法解决如何将其保存到我拥有的SQLite文件中。

I can load data to a tableView from the SQLite file fine as well.. I also now how add to SQLite when using a AddView when you enter data in two textfields. 我也可以从SQLite文件中将数据加载到tableView中。现在,当您在两个文本字段中输入数据时,我还如何在使用AddView时将其添加到SQLite中。 what ever I try it comes up saying out of memory. 我尝试过的所有内容都会显示出内存不足。 I know it be something simple but just cant fine or work this out. 我知道这很简单,但却无法解决。

hope someone can help me with this. 希望有人可以帮助我。

I look forward in hearing back from you 我期待着您的回音

Regarding your memory issue. 关于您的内存问题。 Have you released all your objects? 您释放了所有对象吗? Any time you type Alloc , New or Copy , you are responsible for that object and must release it at some stage in your code. 每当您键入AllocNewCopy时 ,您都要对该对象负责,并且必须在代码的某个阶段将其释放。

Instead of SQLite, use Core Data. 代替SQLite,使用核心数据。 Core Data uses SQLite as its backing store and stores data in an object-orientated way. 核心数据使用SQLite作为其后备存储,并以面向对象的方式存储数据。

You need to select the checkbox for Core Data when creating a project. 创建项目时,需要选中“核心数据”复选框。

Querying Core Data is done as follows: 查询核心数据的操作如下:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"TABLENAME" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"uniqueId = %@", [Data objectForKey:@"id"]];

NSError *error = nil;
NSManagedContext *returnedData = [[context executeFetchRequest:request error:&error] lastObject];
[request release];

Inserting data is done as follows: 插入数据的步骤如下:

[NSEntityDescription insertNewObjectForEntityForName:@"TABLENAME" inManagedObjectContext:context];
    MODEL_OBJECT.uniqueId = [Data objectForKey:@"id"];
    MODEL_OBJECT.title = [Data objectForKey:@"title"];

For a great example, watch lecture 12 (Core data and Table Views) from Stanford's Developing Apps for iOS iTunes U module. 举一个很好的例子,观看斯坦福大学iOS iOS开发应用程序iTunes U模块的第12讲(核心数据和表视图)。

  • MODEL_OBJECT would be the NSManagedObject entity object that is automatically created when you create your NSManagedObjects. MODEL_OBJECT将是在创建NSManagedObjects时自动创建的NSManagedObject实体对象。

Apologise if this doesn't answer your question, but using Core Data is much more efficient than SQL for iPhone Apps. 如果这不能解决您的问题,则表示歉意,但是使用Core Data的效率要比SQL for iPhone Apps高得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM