How to prevent appending \n to NSmutable string while parsing xml?

蓝咒 提交于 2019-12-11 12:11:28

问题


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 \ns 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!