gpt4 book ai didi

ios - 解析XML时如何防止将\n附加到NSmutable字符串?

转载 作者:行者123 更新时间:2023-12-01 17:54:49 24 4
gpt4 key购买 nike

这个问题是this question的扩展。

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

如何防止将 \n添加到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]]];

关于ios - 解析XML时如何防止将\n附加到NSmutable字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com