@synthesize xmlView, xmlData, xmlList, currentItem, xmlValue;


- (void)viewDidLoad {

self.xmlData = [[NSData alloc] init];

self.xmlList = [[NSMutableArray alloc] init];

self.xmlValue = [[NSMutableString alloc] init];

self.currentItem = [[NSMutableDictionary alloc] init];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"xml"];

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

self.xmlData = data;

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.xmlData];

[parser setDelegate:self];

[parser parse];

[parser release];

[xmlView reloadData];

[super viewDidLoad];

}

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

{

if ([elementName isEqualToString:@"d:entry"]) {

elementType = etItem;

}

[self.xmlValue setString:@""];

}


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

{

if(elementType == etItem){

[xmlValue appendString:string];

}

}


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

{

if(elementType != etItem)

return;

if([elementName isEqualToString:@"d:entry"]){

[self.xmlList addObject:[NSDictionary dictionaryWithDictionary:self.currentItem]];

}

else if([elementName isEqualToString:@"d:index"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

else if([elementName isEqualToString:@"h1"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

else if([elementName isEqualToString:@"p"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

NSUInteger row = [indexPath row];

NSDictionary *dt = [self.xmlList objectAtIndex:row];

cell.textLabel.text = [NSString stringWithFormat:@"%@-%@", [dt objectForKey:@"h1"], [dt objectForKey:@"p"]];

return cell;

}


'Object-C' 카테고리의 다른 글

MoviePlayer  (0) 2010.10.09
MapView  (0) 2010.10.09
SQLITE  (0) 2010.10.08
CoreData  (0) 2010.10.07
archiving  (0) 2010.10.07
#include "/user/include/sqlite3.h"

-(NSString *) dataPath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingFormat:@"data.sqlite3""];
}

//applicationDidFinishLaunching 에서 호출 . Table 이 없으면 생성
-(void)createData{
if(sqlite3_open([[self dataPath] UTF8Strin],  &database) != SQLITE_OK){
sqlite3_close(database);
}

char *createSQL = "Create table if not exists address(name text, addr text, tel text, memo text);";

//DDL
if(sqlite3_exec(database, createSQL) != SQLITE_OK)
{
sqlite3_close(database);
}
sqlite3_close(database);
}

//화면이 갱신될때마다 호출
-(void)viewWillAppear:(BOOL) animated{
array = [[NSMutableArray alloc] init];

if(sqlite3_open([[self dataPath] UTF8String], &databae) != SQLITE_OK){
sqlite3_close(database);
}

NSString *selectSQL = @"select * from address";
sqlite3_stmt *stmt; //SQL구문을 컴파일한 바이너리 정보를 담은 구조체

//DML
//sqlite3_prepare_v2 : SQL문을 컴파일해서 sqlite3_stmt 로 만들어줌
if(sqlite3_prepare_v2(database, [selectSQL UTF8String], -1, stmt, nil) == SQLITE_OK){
while(sqlite3_step(stmt) == SQLITE_ROW){
char *nameData = (char *)sqlite3_column_text(stmt, 0);
char *addrData = (char *)sqlite3_column_text(stmt, 1);
char *telData = (char *)sqlite3_column_text(stmt, 2);
char *memoData = (char *)sqlite3_column_text(stmt, 3);

NSString *nameValue = [[NSString alloc] initWithUTF8String:nameData];
NSString *addrValue = [[NSString alloc] initWithUTF8String:addrData];
NSString *telValue = [[NSString alloc] initWithUTF8String:telData];
NSString *memoValue = [[NSString alloc] initWithUTF8String:memoData];

NSDictionary *row = [[NSDictionary alloc] initWithObjectAndKeys:nameValue, @"name", addrValue, @"addr",  telValue, @"tel", memoValue, @"memo", nil];
[array addObject:row];
[row release];
[nameValue release];
[addrValue release];
[telValue  release];
[memoValue release];

}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
[self.tableView reloadData];

[super viewWillAppear:animated];
}

//EditController.m
-(void)viewWillAppear:(BOOL)animated{
txtName.text = @"";
txtAddr.text = @"";
txtTel.text = @"";
txtMemo.text = @"";

if([sMode isEqualToString:@"편집"]){
if(sqlite3_open([[self dataPath] UTF8String], &database) != SQLITE_OK){
sqlite3_close(database);
}

char *selectSQL = "select * from address where name = ? ";
sqlite3_stmt *stmt;

if(sqlite3_prepare_v2(adtabase, selectSQL, -1, stmt, nil) == SQLITE_OK){
sqlite3_bind_text(stmt, 1, [sName UTF8String], -1,  SQLITE_TRANSIENT);

while(sqlite3_step(stmt) == SQLITE_ROW){
char *nameData = (char *)sqlite3_column_text(stmt, 0);
char *addrData = (char *)sqlite3_column_text(stmt, 1);
char *telData = (char *)sqlite3_column_text(stmt, 2);
char *memoData = (char *)sqlite3_column_text(stmt, 3);

NSString *nameValue = [[NSString alloc] initWithUTF8String:nameData];
txtName.text = nameValue;
[nameData release];

NSString *addrValue = [[NSString alloc] initWithUTF8String:addrData];
txtAddr.text = addrValue;
[addrData release];

....
....
}
sqlite3_finallize(stmt);
}
sqlite3_close(database);
}
[super viewWillAppear:animated];
}

-(void)Save{
if(sqlite3_open([[self dataPath] UTF8String], &database) != SQLITE_OK){
sqlite3_close(database);
}

if([sMode isEqualToString:@"추가"]){ 
char *insertSQL = "insert into address(name, addr, tel, memo) values (? , ?, ?, ?);";
sqlite3_stmt *stmt;

if(sqlite3_prepare_v2(database, insertSQL, -1, stmt, nil) == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, [txtName.text UTF8String], -1_;
sqlite3_bind_text(stmt, 2, [txtAddr.text UTF8String], -1_;
sqlite3_bind_text(stmt, 3, [txtTel.text UTF8String], -1_;
sqlite3_bind_text(stmt, 4, [txtMemo.text UTF8String], -1_;
}

if(sqlite3_step(stmt) != SQLITE_DONE){
sqlite3_close(database);
}

sqlite3_finalize(stmt);
}
else if([sMode isEqualToString:@"편집"]){
NSString *updateSQL = [NSString stringWithFormat:@"Update address name = '%@', addr = '%@', tel = '%@', memo = '%@' where name = '%@'",
txtName.text, txtAddr.text, txtTel.text, txtMemo.text, sName];

sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(database, updateSQL, -1, stmt, nil) != SQLITE_OK)
{
sqlite3_close(database);

if(sqlite3_step(stmt) != SQLITE_DONE){
sqlite3_close(database);
}

sqlite3_finalize(stmt);
}

sqlite3_close(database);
[self.navigationController popViewControllerAnimated:YES];
}

'Object-C' 카테고리의 다른 글

MapView  (0) 2010.10.09
XML  (0) 2010.10.09
CoreData  (0) 2010.10.07
archiving  (0) 2010.10.07
pickerView  (0) 2010.10.07
 데이타베이스 코어데이타   
데이타베이스 자체 관리객체 컨텍스트(Managed Object Context)  관리객체를 담는 그릇 
테이블 구조(스키마)  엔티티 (NSEntityDescription)   
테이블 구조를 이루는 세부컬럼 정보 프로퍼티 (Property)   
테이블 내의 하나의 레코드 관리객체 (Managed Object)   
SQL문 (예: select * from) 갬색 요청(Fetch Request)   
SQL 조건문 (where age > 20) 조건 서술(Predicate)   
저장소 관리자 저장소 관리자 (Persistent Store Coordinator) 컨텍스트 내용을 파일이나 DB에 저장하는 객체 




//프로그램종료시 저장
-(void)applicationWillTerminate:(NSNotification *)notification{
coreDataAppDelegate *appDelegate = [UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;

for(int i=1;i<=3;i++)
{
NSString *fieldName = [NSString stringWithFormat:@"line%d",i];
UITextField *field = [self valueForKey:fieldName];

NSFetchRequest *request = [NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
[request setEntity:entity];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
[request setPredicate:pred];

NSManagedObject *theLine = nil;

NSArray *array = [context executeFetchRequest:request error:&error];

if([array count] > 0)
theLine = [array objectAtIndex:0];
else
theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line"                    inManagedObjectContext:context];

[theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];
[theLine setValue:field.text forKey:@"lineText"];

[request release];
}

[context save:&error];
}

-(void)ViewDidLoad{
coreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
[request setEntioty:entity];

NSError *error;

NSArray *array = [context executeFetchRequest:request error:&error];

for(ManagedObject *oneObject in array)
{
NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];
NSString *lineText = [oneObject valueForKey:@"lineText"];

NSString *field  = [NSString stringWithFormat:@"line%d", lineNum];
UITextField *txtField = [self valueForKey:field];

txtField.text = lineText;
}
[request release];

UIApplication *app = [UIApplication sharedApplication];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

}

'Object-C' 카테고리의 다른 글

XML  (0) 2010.10.09
SQLITE  (0) 2010.10.08
archiving  (0) 2010.10.07
pickerView  (0) 2010.10.07
imagePicker  (0) 2010.10.07

+ Recent posts