PDA

View Full Version : showing data from HTML site in UITableView?




alexandergre
Jul 28, 2009, 05:09 PM
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 (http://picasaweb.google.com/lh/photo/Wnsxb11Sq3oGOojT02oB6A?authkey=Gv1sRgCM_qj5eH3ZzMogE&feat=directlink).
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?



PhoneyDeveloper
Jul 28, 2009, 05:20 PM
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.

alexandergre
Jul 28, 2009, 08:54 PM
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.

You mean something like this:


url: http://www.blocket.se/stockholm?ca=11
NSXMLParser *MyXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

can NsXMLParser parse html files??? :confused:

PhoneyDeveloper
Jul 28, 2009, 10:44 PM
can NsXMLParser parse html files??? :confused:

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.

dejo
Jul 29, 2009, 12:07 AM
You mean something like this:


url: http://www.blocket.se/stockholm?ca=11
NSXMLParser *MyXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
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.

can NsXMLParser parse html files??? :confused:
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... :)

firewood
Jul 29, 2009, 02:18 AM
You need to parse the html using the built in xml parser.

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.)

dusker
Jul 29, 2009, 07:12 AM
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.)

would you enlighten me what built in js interpreter you're talking about? i never heard of it :)

alexandergre
Jul 29, 2009, 08:36 AM
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.

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... :)

Thank you very much for your time.
Im going to try it. :) :rolleyes: :apple:

dejo
Jul 29, 2009, 10:05 AM
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.)
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.

dusker
Jul 29, 2009, 12:49 PM
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.

Hi,
how do you invoke javascript functions from ObjC level?
greets

dejo
Jul 29, 2009, 12:56 PM
how do you invoke javascript functions from ObjC level?
One way is by calling UIWebView's stringByEvaluatingJavaScriptFromString:

alexandergre
Jul 29, 2009, 09:58 PM
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

<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&amp;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&amp;w=1">
Dragbil med bergtrailer

</a><br>







600 000:-




</td>

<td class="clean_links">

<a href="http://www.blocket.se/li?ca=2&amp;cg=1220&amp;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&amp;w=1&amp;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.

dejo
Jul 29, 2009, 10:54 PM
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.
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.

cheeloon
Nov 15, 2010, 12:56 AM
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.