从xml服务获取值时出现\ n而不是字符串 [英] \n appearing instead of string while getting values from xml service

查看:437
本文介绍了从xml服务获取值时出现\ n而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过解析通过Websevice返回的xml数据从服务器获取值.

I'm trying to get values from server by parsing the xml data returned through the websevice.

用于解析的代码如下

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    rValue= @"";
    isTag = FALSE;
    if ([elementName caseInsensitiveCompare:@"BusinessName"] == NSOrderedSame)
        isTag=YES;

}


-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{
if (isTag) {
        rValue =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是静态字符串;

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

After printing my array, it looks like this,

(
"Book Store",
"Birch river grill",
PIC,
"Drink Juice",
"Cofee Cafee",
"Diag Block",
"Delici Store",
"\n\n",
"Spoon Fork",
"\n",
"\n\n",
"\n\n\n",
Allow,
"Enjoy Traveling",
"Grwing Three",
"\n",
"\n\n"
)

为什么出现\ n(反斜杠)而不是值(BusinessName)?, 我在浏览器中检查了Web服务,值正在返回 正确地.总共返回了17个元素,您可以看到一些以双qoutes形式记录在日志中的字符串.这不是问题.但是,为什么会出现这种情况.如何获得正确的值而不是反斜杠.

Why the \n(backslashes) are occuring instead of values(BusinessName)?, I checked the webservice in the browser, the values are returning correctly. Totally there are 17 elements returning and you can see some of the strings printed in log without double qoutes. It's not an issue. But, why is that appearing like that. How can I get the correct values instead of backslashes.

  • 我在做什么错了?
  • 如何解决这个问题?

推荐答案

由于每个标签内容可以多次调用foundCharacters,因此这是不正确的:

Since foundCharacters can be called multiple times per content of a tag, this is incorrect:

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{
    if (isTag) {
        rValue =strin;
    }
}

您应该附加它,而不是 strin分配给rValue.当前,实际值(例如@"BusinessName")已得到识别和分配,但随后@"\n\n\n"延续字符串出现在同一标签内,并在首先找到的值之上分配.

Rather than assigning strin to rValue, you should be appending it. Currently, the actual value (say, @"BusinessName") gets recognized and assigned, but then the @"\n\n\n" continuation string comes along inside the same tag, and gets assigned on top of the value that has been found first.

rValue设置为NSMutableString,然后使用appendString,如下所示:

Make rValue an NSMutableString, and use appendString, like this:

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)strin{
    if (isTag) {
        [rValue appendString:strin];
    }
}

请注意,内容末尾也有\n.如果不需要这些结尾字符,则需要手动删除它们,例如,通过调用stringByTrimmingCharactersInSet:并将其作为参数传递给[NSCharacterSet whitespaceAndNewlineCharacterSet]:

Note that the content wold have \ns at the end as well. If you do not need these trailing characters, you would need to remove them manually, for example, by calling stringByTrimmingCharactersInSet:, and passing it [NSCharacterSet whitespaceAndNewlineCharacterSet] as the parameter:

NSString *finalValue = [rValue stringByTrimmingCharactersInSet:
    [NSCharacterSet whitespaceAndNewlineCharacterSet]
];

这篇关于从xml服务获取值时出现\ n而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆