且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从xml服务获取值时出现\ n而不是字符串

更新时间:2021-09-18 06:05:14

由于每个标签内容可以多次调用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]
];