2011-06-01 57 views
0

我正在爲iPhone編寫一個聖經內存應用程序,我需要一些幫助來挑選UIWebView中顯示的經文。從UIWebView中提取XML元素

我有一個讀入UIWebView的xml文件,我需要能夠告訴用戶何時點擊特定的詩句。下面是XML文件的樣本:

<bible translation="KJV"> 
<testament name="Old"> 
    <book name="Genesis"> 
     <chapter number="1"> 
      <verse number="1">In the beginning God created the heaven and the earth.</verse> 
      <verse number="2">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</verse> 
      <verse number="3">And God said, Let there be light: and there was light.</verse> 
      <verse number="4">And God saw the light, that it was good: and God divided the light from the darkness.</verse> 
      <verse number="5">And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.</verse> 

在上面相對於代碼,如果用戶水龍頭「一開始...」的程序需要知道(至少),用戶只需點擊一個「詩句「元素和它的」數字= 1「。

=================== EDITED ========================= =

這裏是我提出的解決方案..類型。有點。 讓我知道它聽起來如何。

我仍然有讀取XML文件的UIWebView。它也被格式化爲捆綁中的CSS文件。由於UIWebViews注意到鏈接(hrefs)[感謝喬納森]我可以在每個標記的詩句前面添加詩句數字(我知道它有點乏味,但我打算繼續做它:)

這樣, xml文件看起來像這樣:

<bible translation="KJV"> 
<testament name="Old"> 
    <book name="Genesis"> 
     <chapter number="1"> 
      <n>1</n><verse number="1">In the beginning God created the heaven and the earth.</verse> 
      <n>2</n><verse number="2">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</verse> 
      <n>3</n><verse number="3">And God said, Let there be light: and there was light.</verse> 
      <n>4</n><verse number="4">And God saw the light, that it was good: and God divided the light from the darkness.</verse> 
      <n>5</n><verse number="5">And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.</verse> 
      <n>6</n><verse number="6">And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.</verse> 
      <n>7</n><verse number="7">And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so.</verse> 

我要麼使用CSS來強制「N」標記作爲鏈接或只是把「href」屬性的元素來代替。 這樣,當經節號碼被發現之前,我可以通過自定義的方法來確定我在哪裏(我在哪個詩句中)。 這裏有一個帖子,我發現關於攔截的鏈接觸發的UIWebView中: http://dblog.com.au/iphone-development/iphone-sdk-tip-firing-custom-events-when-a-link-is-clicked-in-a-uiwebview/

我希望這會工作,它的一個小廣告hockish ..但計劃往往開始的方式。 任何人都可以嘗試這樣的事情嗎?

回答

0

的UIWebView的委託只回應鏈接(的HREF),所以這可能是一個問題。從理論上講,你可以通過做一些命中檢測來解決這個問題,但是這會更明智一點(再加上你晚上會睡得更好:D)來解析XML並將其加載到UITableView中,更易於維護和多用戶友善。

+0

你知道...獲得一些良好的睡眠時間很重要:) 除非我不知道如何調整它,否則UITableView可能在應用程序的外觀和感覺上有點具有挑戰性。這些經文經常會重疊並且位於同一行,並且每個經文都在自己的表格單元格中顯示爲塊狀。不過,我正在完成一個xml解析器的過程,所以如果有一種方法可以將UIWebView的連續外觀變成UITableView,我很樂於接受。 但我並不知道UIWebViews注意到了鏈接。這是一件好事,因爲我也有這個計劃。我會在一秒之內發佈我的想法。 – 2011-06-02 00:59:58

+0

使用NSXMLParser來解析XML。瞭解如何首先解析XML,然後嘗試弄清楚如何使用它與UITableView。你不能試着去學習如何做一些事情,同時試圖把它解決成你正在試圖解決的問題。 – 2011-06-02 06:09:54

+0

你說得對。我開始使用NSXMLParser將xml解析器放在一起,用Testaments-> Books [] - > Chapters [] - > Verses []構建一個聖經對象。當我想要自己顯示一個經文列表時,這對UITableView來說非常方便。 但是,我想使用UIWebView來顯示聖經本身,如下所示: http://www.ebooktechie。com/wp-content/uploads/2008/11/iphonebibleapp.jpg 通過這種方式,詩歌#將鏈接到即將到來的詩節的信息,其餘的文本將是常規的「詩歌」元素。 – 2011-06-02 13:02:15