showing data from HTML site in UITableView?

Discussion in 'iOS Programming' started by alexandergre, Jul 28, 2009.

  1. alexandergre macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #1
    hi. Im new here from another forum. moved here cuz nobody could answer this question:

    1. go here: http://www.blocket.se/stockholm?ca=11
    2. I want to show the items in that page in a UITableView
    3. basicly, something like this.
    4.All the information(Item´s title, thumnail, price, location, uploaded time&date) I need are located in the source code of the page.

    The problem is that I have no clue how to do it.
    I have one guessing though:
    convert the HTML source code to XML.
    in that way I can use xml to show the data in UITableView´s cells.

    can you please help me?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    You need to parse the html using the built in xml parser. Build a data structure that contains all the info you need for the table. That will be something like an array of dictionaries where each dictionary represents a row in the table. Display of this table will be straightforward.
     
  3. alexandergre thread starter macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #3
    You mean something like this:

    Code:
    url: [url]http://www.blocket.se/stockholm?ca=11[/url]
    NSXMLParser *MyXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    can NsXMLParser parse html files??? :confused:
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Yes :D

    I haven't used it for that. You should google and you'll find how others have used it for that and what other solutions there are. But basically the choices are to use an xml parser or regexes and I think you'll find that xml parsers are better for this task. Problem is that html pages may not be well formatted and this can cause the xml parser to have problems. If you only need it to parse a small number of pages then you may be ok.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    That's a start, other than you'll need to redo that assignment of 'url'. You then need to code delegate methods to handle the actual parsing. Look at the SeismicXML sample app for more info on how this works.

    It can. But it'll be much more predictable if the HTML is structured so that it seems like XML (i.e. well-formed). You'll either want to make sure it comes in that way, or figure out some way to convert it to XHTML. Or take your chances... :)
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    The other alternative is to parse the HTML using the built-in Javascript interpreter, which may or may not be a more suitable language for the XML and string processing required. (I like the fact is has built-in regex.)
     
  7. dusker macrumors member

    Joined:
    May 31, 2009
    #7
    would you enlighten me what built in js interpreter you're talking about? i never heard of it :)
     
  8. alexandergre thread starter macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #8
    Thank you very much for your time.
    Im going to try it. :) :rolleyes: :apple:
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Although JavaScript does have regex, it doesn't have a built-in XML parser. Depending on your needs (i.e. parsing and extracting much of the XML vs. pulling out a small number of fields), you need to determine what is more suitable.
     
  10. dusker macrumors member

    Joined:
    May 31, 2009
    #10
    Hi,
    how do you invoke javascript functions from ObjC level?
    greets
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    One way is by calling UIWebView's stringByEvaluatingJavaScriptFromString:
     
  12. alexandergre thread starter macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #12
    So i have been struggling with this app all day long. I have fount a tutorial but cant see a single line in my TableView. anyways... I will continue with this tomorrow.

    You can check the project if you want.



    Every ad has this code: its very bad written. why didnt they use xml. oh gush
    HTML:
    <tr >
    
    <th nowrap="nowrap" class="listing_thumbs_date">
    		Igår<br>23:52
    		
    </th>
    	
    		<td class="listing_thumbs_image">
    				
    			<table cellspacing="0" cellpadding="0" class="clean_table shadowed_thumb">
    				<tbody>
    					<tr>
    						<td><img src="/img/thumb_left_top.gif"></td>
    						<td class="top_middle" valign="bottom" align="left"></td>
    						<td></td>
    					</tr>
    					<tr>
    						<td class="middle_left"></td>
    						<td><a href="http://www.blocket.se/vasterbotten/Dragbil_med_bergtrailer_22815303.htm?ca=2&w=1"><img src="http://195.149.144.149/thumbs/54/5495739903.jpg" border="0" alt="Flera bilder" title="Flera bilder"></a></td>
    						<td valign="top" class="extra_middle_right"><img src="/img/thumb_extra_right_top.gif"></td>
    					</tr>
    					<tr>
    						<td></td>
    						<td valign="top" class="extra_bottom_center"><img src="/img/thumb_extra_left_bottom.gif"></td>
    						<td><img src="/img/thumb_extra_right_bottom.gif"></td>
    					</tr>
    				</tbody>
    			</table>
    		
    		</td>
    		
    		
    		
    		
    		
    		
    		
    		
    		<td nowrap="nowrap" class="thumbs_subject">
    				<a href="http://www.blocket.se/vasterbotten/Dragbil_med_bergtrailer_22815303.htm?ca=2&w=1">
    				Dragbil med bergtrailer
    				
    				</a><br>
    				
    				
    				
    					
    						
    						
    						
    						600 000:-
    					
    					
    				
    				
    			</td>
    
    			<td class="clean_links">
    
    			<a href="http://www.blocket.se/li?ca=2&cg=1220&w=1" tabindex="-1" title="Visa annonser i kategorin Lastbil/Truck/Entreprenad">Lastbil/Truck/Entreprenad</a>
    
    
    			(f) 
    
    			
    
    			<br>	
    
    			
    					<a href="http://www.blocket.se/vasterbotten/Dragbil_med_bergtrailer_22815303.htm?ca=2&w=1&th=mp" tabindex="-1" title="Visa annonsen på karta">Vilhelmina</a>
    				
    			
    			</td>
    			
    		
    	
    </tr>

    Just one question:
    Is it possible to get the HTML source code and save it in a string file?
    that would make every thing 100000% simpler.
     

    Attached Files:

  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    If the HTML is accessible via a URL or a local file, yes. There are a few class methods of NSString that should help you in this regard.
     
  14. cheeloon macrumors newbie

    Joined:
    Nov 14, 2010
    #14
    Hi Alex,

    I also need to do the same thing as you mentioned in this post, could you help ? Fetch data from web or blog and put into UITableView. Thx alot.
     

Share This Page