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. the coding i am using to parser the XML file is:

我正在嘗試設置一個iPhone應用程序,從互聯網上提取XML文件,然后希望它保存到SQLite數據庫。我用來解析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. 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.

我也可以從SQLite文件中將數據加載到tableView中。我現在還可以在兩個文本字段中輸入數據時使用AddView時添加到SQLite。什么我嘗試它出現說內存不足。我知道這很簡單,但不能很好或者解決這個問題。

hope someone can help me with this.

希望有人可以幫助我。

I look forward in hearing back from you

我很期待收到你的回復

1 个解决方案

#1


0  

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.

關於你的記憶問題。你發布了所有物品嗎?無論何時鍵入Alloc,New或Copy,您都要對該對象負責,並且必須在代碼的某個階段將其釋放。

Instead of SQLite, use Core Data. Core Data uses SQLite as its backing store and stores data in an object-orientated way.

而不是SQLite,使用核心數據。 Core Data使用SQLite作為其后備存儲,並以面向對象的方式存儲數據。

You need to select the checkbox for Core Data when creating a project.

創建項目時,您需要選擇Core Data的復選框。

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應用程序講座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比使用iPhone的SQL效率更高。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/03/01/72f2a29746217f8671283184cc6ef66.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com