本文介绍了解析XML时如何防止将\ n附加到NSmutable字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题是此扩展问题.
现在我正在使用以下代码来解析xml数据,
Now I'm using the below code for parsing xml data,
-(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;
rValue
is a NSMutablestring;
在打印阵列后,它看起来像这样,
After printing my array, it looks like this,
(
"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 "
)
如何防止将\n
添加到NSMutable字符串?即,我需要检查字符串是否具有\n
?
How can I prevent adding \n
s to NSMutable string?ie, I need to check the string has \n
or not?
有时这些\ n似乎是NSMutableString的前缀,如有任何建议,我们将不胜感激.
Sometimes these \n are appearing prefix to the NSMutableString, Any suggestions would be appreciated.
推荐答案
尝试更改此方法:
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
if([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame)
[myarray addObject:[rValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
这篇关于解析XML时如何防止将\ n附加到NSmutable字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!