iphone开发之使用touchXml解析xml并且存储到SQLite中
2012-10-30 21:38
525 查看
1.下载touchXml,下载地址:https://github.com/TouchCode/TouchXML
2.然后在项目中新建一个文件夹,然后将Source中的文件
拖入新建的文件夹中。将Copy itmes into destination group’s folder(if needed)勾选上。
3.添加libxml2 library 和 libsqlite3 library
4.配置工程
5.实现功能
2.然后在项目中新建一个文件夹,然后将Source中的文件
拖入新建的文件夹中。将Copy itmes into destination group’s folder(if needed)勾选上。
3.添加libxml2 library 和 libsqlite3 library
4.配置工程
5.实现功能
// // BIDViewController.m // ccccc // // Created by 陈奕龙 on 12-10-30. // Copyright (c) 2012年 浙江大学软件学院. All rights reserved. // #import "BIDViewController.h" #import "TouchXML.h" #import <sqlite3.h> #define kFilename @"danci.sqlite3" @interface BIDViewController () @end @implementation BIDViewController - (void)viewDidLoad { //获得文件路径 NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cet4.xml"]; //取得数据 NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath]; //生成CXMLDocument对象 CXMLDocument *document = [[CXMLDocument alloc] initWithData:XMLData options:0 error:nil ]; //[self parseDire:document]; [self parseDire:document]; [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:kFilename]; //为了拿到sqlite文件才用下面的代码 // NSString *mypath = @"/Users/lingxiubo/Documents"; // return [mypath stringByAppendingPathComponent:kFilename]; } - (void) parseDire:(CXMLDocument *) document { //打开数据库 sqlite3 *database; if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); NSAssert(0,@"Failed to open database"); } NSString *createSQL = @"CREATE TABLE IF NOT EXISTS WORDS" "(ROW INTEGER PRIMARY KEY,WORD TEXT,TRANS TEXT,PHONETIC TEXT);"; char *errorMsg; if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database); NSAssert(0,@"Error creating table :@s", errorMsg); } int row = 1; NSArray *books = NULL; books = [document nodesForXPath:@"//item" error:nil]; for (CXMLElement *element in books) { if ([element isKindOfClass:[CXMLElement class]]) { NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; for (int i = 0; i < [element childCount]; i++) { if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]]) { [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name] ]; } } NSLog(@"%@",[item valueForKey:@"word"]); NSLog(@"%@",[item valueForKey:@"trans"]); NSLog(@"%@",[item valueForKey:@"phonetic"]); NSLog(@"----------%d--------------",row); //插入数据库 char *update = "INSERT OR REPLACE INTO WORDS (ROW,WORD,TRANS,PHONETIC)" "VALUES(?,?,?,?);"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { sqlite3_bind_int(stmt, 1, row); sqlite3_bind_text(stmt, 2,[[item valueForKey:@"word"] UTF8String] , -1, nil); sqlite3_bind_text(stmt, 3,[[item valueForKey:@"trans"] UTF8String] , -1, nil); sqlite3_bind_text(stmt, 4,[[item valueForKey:@"phonetic"] UTF8String] , -1, nil); } if (sqlite3_step(stmt) == SQLITE_DONE) { sqlite3_finalize(stmt); } row = row +1; } } sqlite3_close(database); } @end
相关文章推荐
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- iphone开发 使用TouchJSON框架 解析JSON
- iphone开发 使用TouchJSON框架 解析JSON
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- iOS开发-使用第三方库AFNetWorking解析JSON和XML数据
- IOS 学习笔记17 iPhone开发之使用NSUserDefaults存储数据
- 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&CCMutableDictionary使用与注意!
- xml解析2:使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上
- Android开发存储方式详解之SQLite使用实例
- Android开发之使用PULL解析和生成XML
- iPhone开发 解析xml NSData
- Android中的XML解析-DOM的使用与开发技巧
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- iPhone开发成长之路:SQLite的使用
- 学习iPhone开发中 sqlite3的使用