2010年9月30日木曜日

Objective-C + libxml2 を使ってフィードを取り込みたいんだ (1)

理解するのにかなり苦しみました。
しかもその理解が正しいのかどうか今ひとつ確信がもてず。
でもまぁ気にせずに書いていこう。


前準備
  1. プロジェクト作成
  2. Frameworksにlibxml2.dylibを追加する。
  3. 「プロジェクト設定を編集」でビルドタブの設定の「ヘッダ検索パス」に/usr/include/libxml2を指定する。
  4. 「アクティブターゲット"hoge"を情報」でビルドタブの設定の「ヘッダ検索パス」に${SDKROOT},を指定(}の後ろのカンマも入れること)
  5. 新規ファイル作成でクラスファイル作成(NSObjectのサブクラス、ファイル名をRSSImporter)する。


では始める。
  • 4.で作ったRSSImporterの.h/.mファイルを使った説明がほとんどになる。
  • まずは、libxml2を使うときに定形文みたいに扱われるだろう関数とメソッドを書き上げてみる。
  • 見てみるとわかると思うけど、.mファイルがちょっと変わってる。
  • .mファイルには、C関数の定義と関数自体と構造体、それとクラスエクステンションが余分に書かれてる。
  • これから説明をしていこうと思うけど、あっち行ったりこっち行ったりしながらの話になるから、今どこのこと説明してるのか自分で分からなくなると思うけど、なるべく分かるようにやっていきたいな。


  1. //  
  2. // RSSImporter.h  
  3. //  
  4.   
  5. #import <Foundation/Foundation.h>  
  6. #import <libxml/tree.h>  
  7.   
  8. @interface RSSImporter : NSObject {  
  9. }  
  10.   
  11. @end  


  1. //  
  2. // RSSImporter.m  
  3. //  
  4.   
  5. #import "RSSImporter.h"  
  6. #import <libxml/tree.h>  
  7.   
  8. //  
  9. // libxml2 (XMLのパーサライブラリ) を使うための準備  
  10. // libxml2もlibxml2のAPIもC言語で書かれているのでここはC言語で記述することになる  
  11. //  
  12.   
  13. static void startElementSAX(  
  14.        void *context,  
  15.        const xmlChar *localname,  
  16.        const xmlChar *prefix,  
  17.        const xmlChar *URI,  
  18.        int nb_namespaces,  
  19.        const xmlChar **namespaces,  
  20.        int nb_attributes, int nb_defaulted,  
  21.        const xmlChar **atrributes);  
  22. static void endElementSAX(  
  23.          void *context,  
  24.          const xmlChar *localname,  
  25.          const xmlChar *prefix,  
  26.          const xmlChar *URI);  
  27. static void charactersFoundSAX(  
  28.           void *context,  
  29.           const xmlChar *characters,  
  30.           int lenght);  
  31. static xmlSAXHandler simpleSAXHandlerStruct;  
  32.   
  33. static void startElementSAX(  
  34.        void *context,  
  35.        const xmlChar *localname,  
  36.        const xmlChar *prefix,  
  37.        const xmlChar *URI,  
  38.        int nb_namespaces,  
  39.        const xmlChar **namespaces,  
  40.        int nb_attributes, int nb_defaulted,  
  41.        const xmlChar **atrributes) {  
  42.  // 要素開始の通知を受けたときの処理  
  43. }  
  44.   
  45. static void endElementSAX(  
  46.          void *context,  
  47.          const xmlChar *localname,  
  48.          const xmlChar *prefix,  
  49.          const xmlChar *URI) {  
  50.  // 要素終了の通知を受けたときの処理  
  51. }  
  52.   
  53. static void charactersFoundSAX(  
  54.           void *context,  
  55.           const xmlChar *characters,  
  56.           int lenght) {  
  57.  // 要素間のテキストの通知を受けたときの処理  
  58. }  
  59.   
  60. // SAXハンドラが定義されたxmlSAXHandler構造体に対して、使いたいハンドラを登録する  
  61. // (XMLパース中にXMLの要素や属性を発見したときに通知してほしいハンドラを登録する)  
  62. //  
  63. // 要素間のテキスト  
  64. // エラー  
  65. // 要素の開始  
  66. // 要素の終了  
  67. static xmlSAXHandler simpleSAXHandlerStruct = {  
  68.  NULL,                       /* internalSubset */  
  69.  NULL,                       /* isStandalone   */  
  70.  NULL,                       /* hasInternalSubset */  
  71.  NULL,                       /* hasExternalSubset */  
  72.  NULL,                       /* resolveEntity */  
  73.  NULL,                       /* getEntity */  
  74.  NULL,                       /* entityDecl */  
  75.  NULL,                       /* notationDecl */  
  76.  NULL,                       /* attributeDecl */  
  77.  NULL,                       /* elementDecl */  
  78.  NULL,                       /* unparsedEntityDecl */  
  79.  NULL,                       /* setDocumentLocator */  
  80.  NULL,                       /* startDocument */  
  81.  NULL,                       /* endDocument */  
  82.  NULL,                       /* startElement*/  
  83.  NULL,                       /* endElement */  
  84.  NULL,                       /* reference */  
  85.  charactersFoundSAX,         /* characters */  
  86.  NULL,                       /* ignorableWhitespace */  
  87.  NULL,                       /* processingInstruction */  
  88.  NULL,                       /* comment */  
  89.  NULL,                       /* warning */  
  90.  NULL,                       /* error */  
  91.  NULL,                       /* fatalError //: unused error() get all the errors */  
  92.  NULL,                       /* getParameterEntity */  
  93.  NULL,                       /* cdataBlock */  
  94.  NULL,                       /* externalSubset */  
  95.  XML_SAX2_MAGIC,             //  
  96.  NULL,  
  97.  startElementSAX,            /* startElementNs */  
  98.  endElementSAX,              /* endElementNs */  
  99.  NULL,                       /* serror */  
  100. };  
  101.   
  102. //  
  103. // クラスエクステンションを利用することで、  
  104. // プロパティとメソッドへのアクセスをプライベートからに制限できる  
  105. //  
  106.   
  107. @interface RSSImporter ()  
  108. @end  
  109.   
  110.   
  111. //  
  112. // ここから普通に@implementationです  
  113. //  
  114.   
  115. @implementation RSSImporter  
  116.   
  117. - (void)dealloc {  
  118.  [super dealloc];  
  119. }  
  120.   
  121. #pragma mark NSURLConnection Delegate methods  
  122.   
  123. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
  124.  // データ受信  
  125. }  
  126.   
  127. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
  128.  // 受信完了  
  129. }  
  130.   
  131. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  
  132.  // 受信エラー  
  133. }  
  134.   
  135. @end  


続く。

0 件のコメント:

コメントを投稿