2010-06-29 94 views
0

我想解析一個xhtml文件並在UITableView中顯示。解析xhtml文件的最佳方法是什麼,以便我能夠在瀏覽器中顯示它。這裏是一個示例xhtml源代碼在iPhone SDK解析xhtml?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Wheel</title> 
<link rel="stylesheet" type="text/css" href="../styles/stylesheet.css"/> 
<link rel="stylesheet" type="application/vnd.adobe-page-template+xml" href="../page-template.xpgt"/> 
</head> 
<body> 
<h2 class="h2"><a id="page_x"></a><a id="page_xi"></a><a id="page_xii"></a><a id="table"></a>TABLE OF CONTENTS</h2> 
<p class="content2"><strong><a href="Big Idea.html">WHAT&#8217;S THE BIG IDEA?</a></strong></p> 
<p class="content2"><strong><a href="Choosing.html">CHOOSING</a></strong></p> 
<p class="content2"><strong><a href="author note.html">AUTHOR&#8217;S NOTE</a></strong></p> 
<p class="content2"><strong><a href="part1.html">THE STORY OF OG</a></strong></p> 
<p class="content"><a href="chapter01.html">Chapter &#35;&#160; 1:&#160;&#160;Og Gets an Idea</a></p> 
<p class="content"><a href="chapter02.html">Chapter &#35;&#160; 2:&#160;&#160;Dazed By the Possibilities</a></p> 
<p class="content"><a href="chapter03.html">Chapter &#35;&#160; 3:&#160;&#160;Aargh!</a></p> 
<p class="content"><a href="chapter04.html">Chapter &#35;&#160; 4:&#160;&#160;The Grunt of the Town</a></p> 
<p class="content"><a href="chapter05.html">Chapter &#35;&#160; 5:&#160;&#160;Getting Around To It</a></p> 
<p class="content"><a href="chapter05.html">Chapter &#35;&#160; 6:&#160;&#160;Og&#8217;s Head Is Full, But the Cupboard Is Empty</a></p> 
<p class="content"><a href="chapter07.html">Chapter &#35;&#160; 7:&#160;&#160;The Big Meating</a></p> 
<p class="content"><a href="chapter08.html">Chapter &#35;&#160; 8:&#160;&#160;The Path Is Made By Walking On It</a></p> 
<p class="content"><a href="chapter09.html">Chapter &#35;&#160; 9:&#160;&#160;Crouch</a></p> 
<p class="content"><a href="chapter10.html">Chapter &#35;10:&#160;&#160;An Arrow to the Heart of the Matter</a></p> 
<p class="content"><a href="chapter11.html">Chapter &#35;11:&#160;&#160;Nothing to Get</a></p> 
<p class="content"><a href="chapter12.html">Chapter &#35;12:&#160;&#160;Smoke From a Fire, Flakes From a Stone</a></p> 
<p class="content"><a href="chapter13.html">Chapter &#35;13:&#160;&#160;The Happy Accident</a></p> 
<p class="content"><a href="chapter14.html">Chapter &#35;14:&#160;&#160;The Center Is Everything</a></p> 
<p class="content"><a href="chapter15.html">Chapter &#35;15:&#160;&#160;Sleeping Like a Rock</a></p> 
<p class="content"><a href="chapter16.html">Chapter &#35;16:&#160;&#160;Follow Your Feet</a></p> 
<p class="content"><a href="chapter17.html">Chapter &#35;17:&#160;&#160;Everyone&#8217;s Turn</a></p> 
<p class="content2"><a href="Best Practices.html"><strong>WHAT OG LEARNED:</strong> 12 Wheely Good Best Practices</a></p> 
<p class="content2"><a href="Tooling.html"><strong>TOOLING UP:</strong> 35 Ways to Get the Wheels Turning</a></p> 
<p class="content2"><a href="stone.html"><strong> IN STONE:</strong> The Tools and Techniques Contest</a></p> 
<p class="content2"><a href="Nexting.html"><strong>NEXTING:</strong> the Future</a></p> 
<p class="content2"><a href="Staying.html"><strong> ROLL:</strong> Resources for Thinking Outside the Cave</a></p> 
<p class="content2"><a href="ogcasting.html"><strong>O<span class="smallcaps2">G</span>CASTING:</strong> Your Free Audio Bonus</a></p> 
<p class="content2"><a href="about_author.html"><strong>ABOUT THE AUTHOR</strong></a></p> 
</body> 
</html> 

回答

1

我強烈建議使用libxml2來解析html或xhtml。我遵循本教程http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html,但幾次迭代才能正確執行循環。

另一個好方法是創建一個你自己的web服務,它在服務器端執行xhtml解析並返回結構良好的json結果。通過這種方式,您可以控制解析,以防在HTML結構發生變化時輕鬆更新服務器端的解析器(避免通過其他應用程序更新過程)。

0

另一種方法是使用UIWebView,讓它只用來加載Document並利用DOM Class來訪問數據