我正在尝试解析多级Xml文件,并将xml内容插入Sqldatabase。
我正在使用这个概念。
1 - 我正在创建定义xml标记的类,并将这些标记值初始化为@“null”字符串值。 像这样
@implementation lifeline_detail
@synthesize lifeline_title,lifeline_identifier,created_date,lifeline_image,lifeline_modifienddate;
-(void) lifeline_deatiail_values
{
lifeline_title=@"null";
lifeline_identifier=@"null";
created_date=@"null";
lifeline_image=@"null";
lifeline_modifienddate=@"null";
}
-(void) lifeline_title : (NSString *) xml_value
{lifeline_title=xml_value;
}
-(void) lifeline_identifier : (NSString *) xml_value
{
lifeline_identifier=xml_value;
}
-(void) created_date : (NSString *) xml_value
{
created_date=xml_value;
}
-(void) lifeline_modifienddate : (NSString *) xml_value
{
lifeline_modifienddate=xml_value;
}
-(void) lifeline_image : (NSString *) xml_value
{
lifeline_image=xml_value;
}
-(void) dealloc
{
[lifeline_image release];
[lifeline_title release];
[created_date release];
[lifeline_modifienddate release];
[lifeline_identifier release];
[super dealloc];
}
@end
2-我将此类导入到我的另一个用于解析xml文件的类中,并将数据插入到sqlite数据库中。
那个电话就是那个
.h文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "lifelineemail.h"
#import "lifeline_detail.h"
#import "lifelinephoneno.h"
#import "lifelineaddress.h"
#import "lifeline_geolocation.h"
@class lifeline_geolocation;
@class lifelineaddress;
@class lifelinephoneno;
@class lifelineemail;
@class lifeline_detail;
@class DBAcess;
@interface lifelinexml_parse : NSObject<NSXMLParserDelegate>
{
sqlite3* database;
lifelineemail *life_line_email;
lifelineaddress *life_line_address;
lifeline_detail *life_line_detail;
lifelinephoneno *life_line_phone;
lifeline_geolocation *life_line_geo;
DBAcess *mydatabasepath;
NSMutableString *currentValue;
NSURL *xmlFile;
}
@property (retain,nonatomic) NSURL *xmlFile;
@property (retain,nonatomic) NSMutableString *currentValue;
- (BOOL) parse;
@end
.m文件
#import "lifelinexml_parse.h"
#import "DBAcess.h"
@implementation lifelinexml_parse
@synthesize currentValue,xmlFile;
NSString *xmlPath;
NSString *currentText;
NSString *path;
NSXMLParser *parser;
BOOL parse_finished=NO;
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to Parse life line XML"];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Parse content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
if(mydatabasepath)
{
[mydatabasepath release];
}
else if(!mydatabasepath)
{
mydatabasepath =[[DBAcess alloc] init];
}
path=[mydatabasepath initializeDatabase];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"poi:contact"])
{
if(life_line_email)
{
[life_line_email release];
life_line_email =[[lifelineemail alloc] init];
[life_line_email email_initialization];
}
else if(!life_line_email)
{
life_line_email =[[lifelineemail alloc] init];
[life_line_email email_initialization];
}
if(life_line_address)
{
[life_line_address release];
life_line_address=[[lifelineaddress alloc] init];
[life_line_address address_initialization];
}
else if(!life_line_address)
{
life_line_address=[[lifelineaddress alloc] init];
[life_line_address address_initialization];
}
if(life_line_detail)
{
[life_line_detail release];
life_line_detail=[[lifeline_detail alloc] init];
[life_line_detail lifeline_deatiail_values];
}
else if(!life_line_detail)
{
life_line_detail=[[lifeline_detail alloc] init];
[life_line_detail lifeline_deatiail_values];
}
if(life_line_phone)
{
[life_line_phone release];
life_line_phone=[[lifelinephoneno alloc] init];
[life_line_phone phone_initialization];
}
else if(!life_line_phone)
{
life_line_phone=[[lifelinephoneno alloc] init];
[life_line_phone phone_initialization];
}
if(life_line_geo)
{
[life_line_geo release];
life_line_geo=[[lifeline_geolocation alloc] init];
[life_line_geo geo_initialization];
}
else if(!life_line_geo)
{
life_line_geo=[[lifeline_geolocation alloc] init];
[life_line_geo geo_initialization];
}
}
if ([elementName isEqualToString:@"xal:AddressDetails"])
{
if ([[attributeDict valueForKey:@"AddressType"] length] !=0)
{
[life_line_address address_address_type:[attributeDict valueForKey:@"AddressType"]];
}
}
if ([elementName isEqualToString:@"poi:email"])
{
if ([[attributeDict valueForKey:@"email-type"] length] !=0)
{
[life_line_email email_type:[attributeDict valueForKey:@"email-type"]];
}
}
if ([elementName isEqualToString:@"poi:phone-number" ])
{
if ([[attributeDict valueForKey:@"phone-type"] length] !=0)
{
[life_line_phone phone_type:[attributeDict valueForKey:@"phone-type"]];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(!currentValue)
{
currentValue =[[NSMutableString alloc] initWithString:string];
}
[currentValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"dc:title"])
{
NSString* aString = [NSString stringWithFormat: @"%@", currentValue];
aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString length]!=0)
{
[life_line_detail lifeline_title:aString];
}
}
else if ([elementName isEqualToString:@"dc:identifier"])
{
NSString* aString1 = [NSString stringWithFormat: @"%@", currentValue];
aString1 = [aString1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString1 length]!=0)
{
[life_line_detail lifeline_identifier:aString1];
//[aString1 release];
}
}
else if ([elementName isEqualToString:@"poi:avatar"])
{
NSString* aString88 = [NSString stringWithFormat: @"%@", currentValue];
aString88 = [aString88 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString88 length]!=0)
{
[life_line_detail lifeline_image:aString88];
//[aString88 release];
}
}
else if ([elementName isEqualToString:@"m:created"])
{
NSString* aString2 = [NSString stringWithFormat: @"%@", currentValue];
aString2 = [aString2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString2 length]!=0)
{
[life_line_detail created_date:aString2];
//[aString2 release];
}
}
else if ([elementName isEqualToString:@"m:modified"])
{
NSString* aString3 = [NSString stringWithFormat: @"%@", currentValue];
aString3 = [aString3 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString3 length]!=0)
{
[life_line_detail lifeline_modifienddate:aString3];
//[aString3 release];
}
}
else if ([elementName isEqualToString:@"geo:lat"])
{
NSString* aString4 = [NSString stringWithFormat: @"%@", currentValue];
aString4 = [aString4 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString4 length]!=0)
{
[life_line_geo geo_lat_init:aString4];
//[aString4 release];
}
//[[self saleItem] setGeolati:currentValue];
}
else if ([elementName isEqualToString:@"geo:lon"])
{
NSString* aString5 = [NSString stringWithFormat: @"%@", currentValue];
aString5 = [aString5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString5 length]!=0)
{
[life_line_geo geo_long_init:aString5];
//[aString5 release];
}
//[[self saleItem] setGeolong:currentValue];
}
else if ([elementName isEqualToString:@"xal:CountryName"])
{
NSString* aString6 = [NSString stringWithFormat: @"%@", currentValue];
aString6 = [aString6 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString6 length]!=0)
{
[life_line_address country_name:aString6];
//[aString6 release];
}
//[[self saleItem] setCountryname:currentValue];
}
else if ([elementName isEqualToString:@"xal:LocalityName"])
{
NSString* aString7 = [NSString stringWithFormat: @"%@", currentValue];
aString7 = [aString7 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString7 length]!=0)
{
[life_line_address locality_name:aString7];
//[aString7 release];
}
//[[self saleItem] setLocalityname:currentValue];
}
else if ([elementName isEqualToString:@"xal:DependentLocalityName"])
{
NSString* aString8 = [NSString stringWithFormat: @"%@", currentValue];
aString8 = [aString8 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString8 length]!=0)
{ [life_line_address dependentlocality_name:aString8];
//[aString8 release];
}
//[[self saleItem] setDependentlocalityname:currentValue];
}
else if ([elementName isEqualToString:@"xal:ThoroughfareName"])
{
NSString* aString9 = [NSString stringWithFormat: @"%@", currentValue];
aString9 = [aString9 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString9 length]!=0)
{
[life_line_address thoroughfare_name:aString9];
//[aString9 release];
}
//[[self saleItem] setThoughfarename:currentValue];
}
else if ([elementName isEqualToString:@"xal:AddressLine"])
{
NSString* aString10 = [NSString stringWithFormat: @"%@", currentValue];
aString10 = [aString10 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString10 length]!=0)
{
[life_line_address postal_code:aString10];
//[aString10 release];
}
//[[self saleItem] setAddressline:currentValue];
}
else if ([elementName isEqualToString:@"poi:phone-country-code"])
{
NSString* aString11 = [NSString stringWithFormat: @"%@", currentValue];
aString11 = [aString11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString11 length]!=0)
{
[life_line_phone county_code:aString11];
//[aString11 release];
}
}
else if ([elementName isEqualToString:@"poi:phone-area-code"])
{
NSString* aString12 = [NSString stringWithFormat: @"%@", currentValue];
aString12 = [aString12 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString12 length]!=0)
{
[life_line_phone area_code:aString12];
//[aString12 release];
}
//[[self saleItem] setAreacode:currentValue];
}
else if ([elementName isEqualToString:@"poi:phone-subscriber"])
{
NSString* aString13 = [NSString stringWithFormat: @"%@", currentValue];
aString13 = [aString13 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString13 length]!=0)
{
[life_line_phone phone_no:aString13];
//[aString13 release];
}
//[[self saleItem] setPhoneno:currentValue];
}
else if ([elementName isEqualToString:@"poi:email"])
{
NSString* aString14 = [NSString stringWithFormat: @"%@", currentValue];
aString14 = [aString14 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString14 length]!=0)
{
[life_line_email lifeline_email:aString14];
//[aString14 release];
}
//[[self saleItem] setEmail_s:currentValue];
}
else if ([elementName isEqualToString:@"poi:contact"])
{
NSInteger lifelineinsertrowid;
sqlite3_stmt *statement2;
sqlite3_stmt *statement3;
sqlite3_stmt *statement4;
sqlite3_stmt *statement5;
// NSLog(@"%@",life_line_detail.lifeline_identifier);
if (sqlite3_open([path UTF8String], & database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO lifeline (pn_lifeline_identifier,lifeline_contact_name,lifeline_latitude,lifeline_longitute,lifeline_images,distance) Values (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%f\")",life_line_detail.lifeline_identifier ,life_line_detail.lifeline_title,life_line_geo.geo_latitude,life_line_geo.geo_longitude ,life_line_detail.lifeline_image,0.00];
if (sqlite3_prepare_v2(database, [insertSQL UTF8String], -1, &statement2, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(statement2))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
{
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
lifelineinsertrowid = sqlite3_last_insert_rowid(database);
if (lifelineinsertrowid>0)
{
NSString *insertSQL1 = [NSString stringWithFormat: @"INSERT INTO lifeline_contact (lifeline_id ,address_type,lifeline_country,lifeline_locality,lifeline_dependentlocality,lifeline_throughfare,lifeline_pastalcode) Values (\"%d\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", lifelineinsertrowid,life_line_address.address_type ,life_line_address.country_name ,life_line_address.locality_name,life_line_address.dependentlocality_name ,life_line_address.thoroughfare_name ,life_line_address.postal_code];
if (sqlite3_prepare_v2(database, [insertSQL1 UTF8String], -1, &statement3, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(statement3))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
{
NSString *insertSQL2 = [NSString stringWithFormat: @"INSERT INTO lifeline_emails (lifeline_id ,lifeline_emailtype,lifeline_email) Values (\"%d\", \"%@\", \"%@\")", lifelineinsertrowid,life_line_email.email_type ,life_line_email.lifeline_email];
if (sqlite3_prepare_v2(database, [insertSQL2 UTF8String], -1, &statement4, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(statement4))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
{
NSString *insertSQL3 = [NSString stringWithFormat: @"INSERT INTO lifeline_phones (lifeline_id ,country_code,area_code,lifeline_phoneno,lifeline_phonetype) Values (\"%d\", \"%@\", \"%@\",\"%@\",\"%@\")", lifelineinsertrowid,life_line_phone.county_code,life_line_phone.area_code,life_line_phone.phone_no,life_line_phone.phone_type];
if (sqlite3_prepare_v2(database, [insertSQL3 UTF8String], -1, &statement5, NULL) == SQLITE_OK)
{
if(SQLITE_DONE == sqlite3_step(statement5))
{
//NSLog(@"Insert all data");
}
else
{
NSLog(@"Error to insert");
}
}
sqlite3_reset(statement5);
}
}
sqlite3_reset(statement4);
}
}
sqlite3_reset(statement3);
}
}
}sqlite3_reset(statement2);
sqlite3_close(database);
}
else
{
sqlite3_close(database);
NSAssert1(0, @"Failed to open database: '%s'.",sqlite3_errmsg(database));
}
}
[currentValue release];
currentValue=nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%x",[life_line_email retainCount]);
parse_finished=YES;
}
- (BOOL) lifeLineParsing
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
xmlPath= [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/uncompressddatafolder/lifelines.xml"];
NSData *xml = [NSData dataWithContentsOfFile:xmlPath];
parser = [[NSXMLParser alloc] initWithData:xml] ;
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO]; // We don't care about namespaces
[parser setShouldReportNamespacePrefixes:NO]; //
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser setDelegate:nil];
[parser release];
return parse_finished;
}
- (void) dealloc
{
if(life_line_address)
{
[life_line_address release];
life_line_address=nil;
}
if(life_line_geo)
{[life_line_geo release];
life_line_geo=nil;
}
if (life_line_phone) {
[life_line_phone release];
life_line_phone=nil;
}
if (life_line_detail) {
[life_line_detail release];
life_line_detail=nil;
}
if (life_line_email) {
[life_line_email release];
life_line_email=nil;
}
if (mydatabasepath) {
[mydatabasepath release];
mydatabasepath=nil;
}
[super dealloc];
}
@end
当我采用这种类型的概念时,我是对还是错。
因为我的应用程序难以理解此错误
- [CFString release]:发送到解除分配的实例0x11815530的消息
我真的找不到我错在哪里。
请帮助我。
过去20天我遇到了这个问题。
请帮帮我
答案 0 :(得分:1)
班级名称以大写字母开头
方法名称为camelCasedAndDoNotContainUnderscores
尝试“构建和分析”;修复它识别的任何问题
retainCount无用。不要打电话。
setter方法以单词set
开头,不包含下划线; setMyName:
,例如
您的所有setter方法都未正确实现;直接设置值而不保留任何内容。正如迈克所说,请阅读memory management documentation guide。
parserDidStartDocument:
的实施没有意义;如果设置了myDatabasePath
(重命名为遵循约定),它将成为一个悬空指针并崩溃。
由于不知道Objective-C是nil-eats-message
语言,因此存在大量不必要的代码。我也建议你也阅读Objective-C concepts document。