如何使用TouchXML或其他库在iPhone上解析HTML?

时间:2011-10-20 18:30:33

标签: iphone parsing ios5 touchxml hpple

我有一个从外部服务器加载的脏HTML代码(因此我无法创建json文件或清除html代码)。我的HTML结构如下:

<!-- 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>

        &middot;

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

我想使用xPath <div class="pic" id="*">选择所有//div[@class='pic'][@id]

以下是我使用的两个库:

- Hpple
- TouchXML

至于Hpple - &gt;它很棒,但我不能选择一个emelent innerHTML。至于TouchXML,我用它来解析XML,它很棒。但是它无法解析脏HTML - 我收到了很多错误。

有没有办法使用TouchXML在iOS5中解析这个HTML?它可以是一个不同的库,但我更喜欢那个。

我听到了关于CTidy.h的一些事情,我按照指示做了但没有改变......

2 个答案:

答案 0 :(得分:4)

libxml有一个专门针对这个问题设计的模块:)

http://xmlsoft.org/html/libxml-HTMLparser.html

它与libxml normally works完全相同,即解析包含脏html的NSData对象:

#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);

与其网站上的libxml示例相比:

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不要忘记将libxml2.dylib作为'link binary with libraries'项目构建阶段的框架包含在项目中

答案 1 :(得分:0)

如果我这样做,我会在传递它之前解析HTML并删除所有'脏'位找到并删除之间的所有内容,对其他脏区执行相同操作然后它将是库更容易使用该文件。