如何防止在解析xml时将\ n附加到NSmutable字符串?

这个问题是这个问题的延伸。

现在我使用下面的代码来解析xml数据,

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { rValue = [[NSMutableString alloc]init]; isTag = FALSE; if ([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame) isTag=YES; } -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{ if (isTag) [rValue appendString:strin]; } -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame) [myarray addObject:rValue]; } -(void) parserDidEndDocument:(NSXMLParser *)parser { NSLog(@"%@",myarray); } 

rValue是一个NSMutablestring;

打印完arrays后,它看起来像这样,

 ( "Book Store\n ", "Birch river grill\n ", "PIC\n ", "Drink Juice\n ", "Cofee Cafee\n ", "Diag Block\n ", "Delici Store\n ", "Rubbit Rabbit\n\n\n ", "Spoon Fork\n ", "Travel Sea\n\n ", "Water Melocious\n\n\n ", "Lazee Fish\n\n\n\n ", "Allow\n ", "Enjoy Traveling\n ", "Grwing Three\n ", "Diz Cow\n\n ", "Wheeler Marine\n\n\n " ) 

如何防止将NS添加到NSMutable字符串? 即,我需要检查字符串是否具有\n

有时这些\ n出现在NSMutableString的前缀,任何建议将不胜感激。

尝试更改此方法:

  -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName if([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame) [myarray addObject:[rValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 

修改你的条件: –

 -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{ if (isTag) { NSString *newStr=[strin stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] [rValue appendString:newStr]; } }