I have a dirty HTML code that is loaded from a foreign server (so I can't make a json file or clean the html code). My HTML's structure is like:
<!-- SOME DIRTY HTML, CSS, JS, AND OTHER STUFF -->
<div class="pic"> ... </div>
<div class="pic" id="pic311809">
<input type="hidden" class="pic_id" name="pic_id" value="311809" />
<!-- tylko komixxy.pl -->
<div style="font-family: verdana, arial, helvetica, sans-serif; font-weight: bold; font-size: 9px;">
<a href="pic/show_series/1">FFFUUU (rageman)</a>
</div>
<h1 class="picture">Kochana babcia</h1>
<div class="infobar">
Wrzucone 15 października 2010 o 16:03 przez <a href="/user/Astraly">Astraly</a>
|
<a href="http://komixxy.pl/311809/Kochana-babcia#comments">Skomentuj (23)</a>
<!-- głosowanie przeniesione pod spód obrazka -->
</div><!-- .infobar -->
<div class="pic_image">
<a href="http://komixxy.pl/311809/Kochana-babcia"><img src="http://staticrps.komixxy.pl/uimages/201010/1287151388_by_Astraly_500.jpg" class="pic" alt="Kochana babcia - Wnusiu, a ty jeszcze nie w szkole? Dziś mamy na 10 babciu Co ty tam majaczysz? Jesteś na wagarach!? już ja to powiem twojej mamie! Ale babciu.... Przynosisz nam wstyd! Myślisz, że nie wiem o tej ostatniej niedzieli, w której nie byłeś u komunii? ZAMKNIJ SIĘ KU**A!!!! .... Nie musisz tak krzyczeć! Powiem twojej mamie z jakim tonem odnosisz się do mnie! " /></a> </div><!-- .pic_image -->
<div class="source">Źródło: Kto mieszka z babcią, ten wie jak to jest ;)</div>
<!-- głosowanie i ocena -->
<div class="source">
<div class="infobar center">
Głosuj:
<a href="/pic/vote/311809/up"
onclick="votowanie(this); return false;"
class="vote voteup iconlink"
>
mocne ↑ </a>
·
<a href="/pic/vote/311809/down"
onclick="votowanie(this); return false;"
class="vote votedown iconlink"
>
słabe ↓ </a>
<!-- DODATKOWY PRZYCISK RAPORTOWANIA DUPLIKATÓW (“BYŁO”) -->
|
<span class="points">
87% mocnych
</span>
<span class="count">
z 1291 głosów
</span>
<span class="vote_result"></span>
| <a href="/user/add_favorite/311809" class="favorite">Do ulubionych</a>
</div><!-- .infobar -->
<!-- PRZYCISK LAJKONIKA -->
<div style="text-align: center;">
<fb:like href="http://komixxy.pl/311809/Kochana-babcia"
layout="button_count"
show_faces="true"
width="130"
font="arial"
style="width: 130px;">
</fb:like>
</div>
<!-- tylko komixxy.pl -->
<a href="http://komixxy.pl/pic/show_group/311809" class="picbutton">Pokaż podobne komixxy</a> <a href="http://komixxy.pl/przerob/311809" class="picbutton">Zrób własną wersję</a>
<div style="clear: both;"></div>
</div><!-- .source -->
</div><!-- .pic -->
<div class="pic"> ... </div>
<div class="pic"> ... </div>
<div class="pic"> ... </div>
I want to select all <div class="pic" id="*">
by using xPath //div[@class='pic'][@id]
.
Here are two libraries that I used:
- Hpple
- TouchXML
As for Hpple -> it's great but I can't select innerHTML
of an emelent. As for TouchXML, I use it for parsing XML and it's great. But it doesn't manage to parse dirty HTML - I get dozens of errors.
Is there a way to parse this HTML in iOS5 using TouchXML? It can be a different library, but I prefer that one.
I heard something about CTidy.h
and I did as instructed but nothing's changed...
libxml has a module designed exactly for this problem :)
http://xmlsoft.org/html/libxml-HTMLparser.html
It works exactly the same as libxml normally works i.e. to parse an NSData
object containing dirty html:
#include <libxml/htmlparser.h>
htmlDocPtr doc; /* the resulting document tree */
doc = htmlReadMemory([data bytes], [data length], "noname.xml", NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
if (NULL == doc)
return nil;
... parse DOM here ...
xmlFreeDoc(doc);
compared to the libxml example from their website :
xmlDocPtr doc; /* the resulting document tree */
doc = xmlReadMemory(content, length, "noname.xml", NULL, 0);
if (NULL == doc)
return nil;
... parse DOM here ...
xmlFreeDoc(doc);
PS Don't forget to include libxml2.dylib into your project as a framework in the 'link binary with libraries' project build phase