在解析数据时,我在sqlite中插入数据。但由于某种原因,当我运行SELECT查询时,它会返回额外的行和制表符空间。当我编写此查询时,它会正确插入数据,但会有额外的空格。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqual:@"root"]) {
}
else if([elementName isEqualToString:@"value"]) {
value = [[Value alloc] init] ;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!trueValue)
trueValue = [[NSMutableString alloc] initWithString:string];
else{
[trueValue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"root"])
return;
if([elementName isEqualToString:@"value"]) {
NSString *sqlQuery = [NSString stringWithFormat:@"INSERT OR REPLACE INTO DETAIL(ID,KEY, TITLE) VALUES ('%@','%@','%@');", key, parent_key, title];
const char *insert_sql = [sqlQuery UTF8String];
sqlite3_exec(db, insert_sql, NULL, NULL, NULL);
}
else if([elementName isEqualToString:@"key"])
[value setValue:finalValue forKey:elementName]
[trueValue release];
trueValue = nil;
}
如何更换多余的空格基本上如下:“\ n \ t \ t \ t”在所有行的此语句之前带有“”?
答案 0 :(得分:1)
我有一个类似的问题,我通过修剪所有的空格字符来解决这个问题:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!trueValue)
trueValue = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
else
[trueValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
答案 1 :(得分:0)
试试这个:
[string stringByReplacingOccurrencesOfString:@“\ n \ t \ t \ t”withString:@“”]