在使用nsxmlparser解析rss源时,如何忽略非法字符?

时间:2012-04-17 06:34:28

标签: ios rss nsxmlparser

使用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文档,该文档似乎是Feed中的非法字符。

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源不会失败?

参考文献:(我已经调查过的链接)

HTML character decoding in Objective-C / Cocoa Touch

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我在解析从我的Enigma2接收器的REST API中获取的EPG Data时发现了类似的东西。在这种情况下,一个服务正在使用非法字符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中。

相关问题