问题
This problem is the extension of this question.
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
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 "
)
How can I prevent adding \n
s to NSMutable string?
ie, I need to check the string has \n
or not?
Sometimes these \n are appearing prefix to the NSMutableString, Any suggestions would be appreciated.
回答1:
try to change this method:
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
if([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame)
[myarray addObject:[rValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
回答2:
Modified your condition :-
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{
if (isTag)
{
NSString *newStr=[strin stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
[rValue appendString:newStr];
}
}
来源:https://stackoverflow.com/questions/19705695/how-to-prevent-appending-n-to-nsmutable-string-while-parsing-xml