2011-05-08 63 views
0

我正在編寫一個從Flickr API下載數據的iPhone應用程序。目前似乎沒有辦法限制它下載多少個comments,儘管我想得到8或10個,但它有時會給我上百個。我有subclassedASIHTTPRequest,這樣它只會下載一定數量的字節(例如,在接收到1024字節的評論數據後它將停止下載)。目標C:解析格式不正確的XML

現在,我想解析的信息都在那裏(註釋數據包含諸如用戶ID,文本等內容)。然而,由於它在結束之前被切斷,所以XML格式不正確,而且我當前的解決方案(使用ObjectiveFlickr的XML解析器)無法解析XML。有沒有辦法處理格式不正確的XML,舊的Web瀏覽器處理HTML的方式,只提取格式正確的數據?

下面是一些樣本數據:

<?xml version="1.0" encoding="utf-8" ?> 
<rsp stat="ok"> 
<comments photo_id="5692627867"> 
    <comment id="49862655-5692627867-72157626659891768" author="[email protected]" authorname="eαse*" iconserver="4046" iconfarm="5" datecreate="1304689286" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626659891768">lovely lovely lovelyyyyy!!! ♥♥♥♥♥♥♥♥♥</comment> 
    <comment id="49862655-5692627867-72157626535581359" author="[email protected]" authorname="RandomPics Art" iconserver="4017" iconfarm="5" datecreate="1304692593" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626535581359">nice! like soft colors and tones!</comment> 
    <comment id="49862655-5692627867-72157626660240896" author="[email protected]" authorname="kiki_chi" iconserver="4014" iconfarm="5" datecreate="1304693051" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660240896">&amp;gt;eαse* 
&amp;gt;RandomPics Art 

Thank you:) :) :)</comment> 
    <comment id="49862655-5692627867-72157626660761230" author="[email protected]" authorname="petia.bourova" iconserver="4082" iconfarm="5" datecreate="1304698244" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660761230">Thank you!Very nice photo!I like The coulers very,very much!!!</comment> 
    <comment id="49862655-5692627867-72157626661258700" author="[email protected]" authorname="Leentje32" iconserver="4067" iconfarm="5" datecreate="1304703576" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626661258700">Aww so lovely!! Beautiful capture.</comment> 
    <comment id="49862655-5692627867-72157626662413410" author="[email protected]" authorname="My NIKON And Me" iconserver="5310" iconfarm="6" datecreate="1304716098" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626662413410">lovely image!!!</comment> 
    <comment id="49862655-5692627867-72157626663408864" author="[email protected]" authorname="Majlee" iconserver="3130" iconfarm="4" datecreate="1304728344" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663408864">This is just adorable !</comment> 
    <comment id="49862655-5692627867-72157626663519092" author="[email protected]" authorname="mr_jyoti" iconserver="4011" iconfarm="5" datecreate="1304729940" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663519092">Cool shot. Nice bokey.</comment> 
    <comment id="49862655-5692627867-72157626663642456" author="[email protected]" authorname="my beanie hat rocks" iconserver="2550" iconfarm="3" datecreate="1304731810" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663642456">Maybe she could cheer this fella up!! 

&lt;a href=&quot;http://www.flickr.com/photos/weasteman/5652855802/in/photostream&quot;&gt;www.flickr.com/photos/weasteman/5652855802/in/photostream&lt;/a&gt; 

=D</comment> 

回答

1

我不知道確切的XML格式,但它看起來很簡單。在這種情況下,您可以嘗試找出數據中的最後一個標記並手動添加缺失的結束標記。它不應該超過簡單的字符串搜索&替換。