1

hpple を使用して HTML ドキュメントを解析しています。私は Ray Wenderlich のチュートリアルに従い、サンプル ファイルに対してすべて正常に動作しました。ただし、友人のブログの特定の HTML ファイルを読み取るには、少し変更する必要があります。このファイルは、これまでに使用した例よりも複雑です。ファイルの関連部分 ( gistに完全にアップロードされたものは次のとおりです。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<!-- snip -->
<div id="content" class="hfeed">
            <div class="post-21443 post type-post status-publish format-standard hentry category-about-catherine">

      <div class="postdate">
      Apr          <br />
      6            <br />
      2013         
      </div>
    <h2 class="entry-title"><a href="http://catherinepooler.com/2013/04/stampnation-live-retreat-updates/" title="StampNation LIVE Retreat Updates" rel="bookmark">StampNation LIVE Retreat Updates</a></h2>

    <div class="post-info"></div>       <div class="entry-content">
        <p><a href="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560.jpg" ><img class="aligncenter size-large wp-image-21444" alt="StampNation LIVE" src="http://catherinepooler.com/wp-content/uploads/2013/04/IMG_0560-450x337.jpg" width="450" height="337" /></a></p> <p>StampNation LIVE is in full swing!  We are having a wonderful time.  I am taking a quick break from stamping and chatting to share a few photos with you.</p> <p>I think my favorite thing in getting ready for the retreat was setting up the Accessory Bar.  Each attendee received a small galvanized bucket with their fully glittered initial on it to fill up at the bar.  Awesome!</p>
<!-- snip -->

ファイル内にはこれらのセクションがいくつかあり、すべてのセクションを配置する必要があります。

<h2 class = "entry-title"> 

(title="StampNation LIVE Retreat Updates") 配列で。無事に配置できました

<div class = "entry-content"> 

XPathQuery を使用して配列に変換し//div[@class = 'entry-content']/pます。ただし、空の配列が原因でコードがクラッシュせずにタイトルを取得できないようです。明らかに、私の XPathQuery は正しくありません。これは私が試したものです。

//h2[@class = 'entry-title']  (: this crashed :)

//div[@class = 'post-21443.....']//h2[@class = 'entry-title']  (: this crashed too.   ")

他の多くの試みと一緒に!

誰か私にアドバイスはありますか?多くのSOの回答とhppleに付属の例を調べましたが、つなぎ合わせることができません。

更新: Jens の助けを借りて、クエリを
NSString *postsXpathQueryString = @"//h2[@class = 'entry-title']/a"; に変更しました。

これにより配列が取得されますが、このエラーも発生します。

2013-04-08 10:26:30.604 HTML[12408:11303] * キャッチされない例外 'NSRangeException' が原因でアプリを終了します。理由: '* -[__NSArrayM objectAtIndex:]: 境界を超えたインデックス 4 [0 .. 3]' * First throw call stack: (0x210a012 0x1203e7e 0x20ac0b4 0x3852 0x2028fb 0x2029cf 0x1eb1bb 0x1fbb4b 0x1982dd 0x12176b0 0x2706fc0 0x26fb33c 0x2706eaf 0x2372bd 0x17fb56 0x17e66f 0x17e589 0x17d7e4 0x17d61e 0x17e3d9 0x1812d2 0x22b99c 0x178574 0x17876f 0x178905 0x9733ab6 0x181917 0x14596c 0x14694b 0x157cb5 0x158beb 0x14a698 0x2065df9 0x2065ad0 0x207fbf5 0x207f962 0x20b0bb6 0x20aff44 0x20afe1b 0x14617a 0x147ffc 0x1d2d 0x1c55) libc++abi.dylib: 例外をスローして呼び出された終了

更新 2

reloadData 時に if ステートメントを挿入することで、範囲外のエラー インデックスを修正しました。NSLog で配列を取得しましたが、テーブル ビューに配置されていません。テーブルビューが空になります!! しかし、もうクラッシュしません!!!

最終更新

Jensはクエリを正しくするのを手伝ってくれたので、テーブルビューに入力するだけで済みました。Ray の tut には無数のエントリがあったため、配列カウントを 20 に設定しました。私の友達のブログは4つしかありませんでした!すべての助けをありがとう。

4

1 に答える 1