您的位置:首页 > 数据库

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.实现功能

//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐