2

使用 NSXMLParser(间接通过 Michael Waterfalls MWFeedParser 库)并解析以下 RSS 提要时:

http://qdb.us/qdb.xml?action=latest

NSURL *feedURL = [NSURL URLWithString:@"http://qdb.us/qdb.xml?action=random"];
self.feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
self.feedParser.delegate = self;
self.feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items
self.feedParser.connectionType = ConnectionTypeAsynchronously;
[self.feedParser parse];

我收到了一个格式无效的 xml 文档,该文档似乎是提要中的非法字符。

http://validator.w3.org/check?uri=http%3A%2F%2Fqdb.us%2Fqdb.xml%3Faction%3Dlatest&charset=utf-8&doctype=Inline&group=0&user-agent=W3C_Validator%2F1.1

我尝试将文档编码从 ISO-8859-1 更改为 UTF-8,但问题仍然存在。

如何识别非法字符,然后如何使它在遇到这些非法字符时解析 RSS 提要不会失败?

参考资料:(我已经调查过的链接)

Objective-C / Cocoa Touch 中的 HTML 字符解码

https://stackoverflow.com/users/106244/michael-waterfall

4

2 回答 2

0

我不知道如何忽略非法字符,但您可能会考虑在解析之前进行一些正则表达式更正以删除它们,但我建议使用 nsxmlparser 的 killxml instand,这对于非法字符可能没问题,这里是“如何选择iPhone 项目的最佳 XML 解析器”

于 2012-04-26T01:00:31.820 回答
0

我在解析从我的 Enigma2 接收器的 REST API 抓取的 EPG 数据时发现了类似的东西。在这种情况下,一项服务正在推送带有非法字符 0x05 的 EPGInfo。
我已经为传入的 NSData 实现了一种清理方法。这是从我从 NSURLSession 收到的 NSData 中过滤这些 0x05 字节的穷人的方法,然后再将其传递给解析器:

-(NSData *)DataCleaned:(NSData *)data {
   NSData *clean = nil;
   const char *old = (const char *)data.bytes;
   char *flt = (char *)calloc( data.length, sizeof( char ) );
   NSInteger cnt = 0;
   for( NSInteger i = 0; i < data.length; i++ ) {
      if ( old[i] != 0x05 )
         flt[cnt++] = old[i];
   }
   clean = [NSData dataWithBytes:flt length:cnt];
   free( flt );
   return clean;
}

就我而言,这解决了问题。但这当然需要在解析之前将响应加载到 NSData 中。

于 2017-01-08T14:35:08.240 回答