Iphone 解析时如何从xml文件中删除空格
我使用了推荐的代码行:Iphone 解析时如何从xml文件中删除空格,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我使用了推荐的代码行: - (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string { // save the characters for the current item... if ( [currentElement isEqualToString:@"title"]) { currentNodeContent = [[NSMutableString alloc]init];
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"])
{
currentNodeContent = [[NSMutableString alloc]init];
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
[currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
NSLog(@"currentnode content...%@",currentNodeContent);
[blogtitle addObject:currentNodeContent];
NSLog(@"%@",blogtitle);
}
我仍然得到数组中的空值。我猜字符串“currentNodeContent”的初始化应该在其他地方。在您的
NSXMLParserDelegate
中检查并仅添加非空字符串,例如[currentNodeContent length]>0
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"])
{
NSString *newString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([newString length] > 0)
{
[blogtitle addObject:newString];
}
NSLog(@"currentnode content...%@",currentNodeContent);
NSLog(@"%@",blogtitle);
}
只是不要将空字符串添加到数组中。在添加之前检查长度。你应该存储修剪过的字符串,以避免修剪两次。感谢你的提醒Georg,我的意思是检查字符串的长度,我没有想到代码,我感谢你的评论,以便我可以改进我的回答方式….,编辑了答案