1

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

现在我使用下面的代码来解析 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;

打印我的数组后,它看起来像这样,

(
"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 的前缀,任何建议将不胜感激。

4

2 回答 2

4

尝试更改此方法:

 -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

 if([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame)
  [myarray addObject:[rValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
于 2013-10-31T11:51:29.277 回答
1

修改了您的条件:-

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{
 if (isTag) 
{
  NSString *newStr=[strin stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
     [rValue appendString:newStr];
}
    }
于 2013-10-31T12:06:51.863 回答