Android的:如何解析HTML表成的ListView? [英] Android: How to parse HTML Table into ListView?

查看:356
本文介绍了Android的:如何解析HTML表成的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想解析HTML表格变成ListView中的Andr​​oid,但我不知道从哪里开始。表中有大量的信息。可能有人帮助我下手呢?

Hello i want to parse a HTML Table into a Android ListView but i don't know where to start. The Table has a lot of information. Could someone help me to start with this?

在此先感谢!

HTML表:的http://intranet.staring.nl/toepassingen/rooster/lochem/2W2/2012090320120909/2W01533.htm (只需点击查看源文件)。

The HTML Table: http://intranet.staring.nl/toepassingen/rooster/lochem/2W2/2012090320120909/2W01533.htm (Just click view source).

推荐答案

我不知道,如果你已经得到了你的答案在这里,但我做了你的建议的链接一样,我将在这里发布我的code,但它仍然是非常混乱和不适用的最新时间表(9小时)

I don't know if you already got your answer here but I did the same with the link you suggest, I will post my code here but it is still very messy and don't apply for the newest timetable(9th hour)

林:

try {   
        HtmlCleaner hc = new HtmlCleaner();
        CleanerProperties cp = hc.getProperties();
        cp.setAllowHtmlInsideAttributes(true);
        cp.setAllowMultiWordAttributes(true);
        cp.setRecognizeUnicodeChars(true);
        cp.setOmitComments(true);

        String loc = sp.getString( Constants.pref_locatie      , "" );
        String per = sp.getString( Constants.pref_persoon      , "" );
        String oob = sp.getString( Constants.pref_onderofboven , "" );

        int counteruurmax;
        int[] pauze;
        if (oob.contains("onder")){
            pauze = Constants.pauzeo;
        } else if (oob.contains("boven")) {
            pauze = Constants.pauzeb;
        } else {
            return false;
        }

        String url = "";
        if (loc.contains("lochem")) {
            url += Constants.RoosterLochem;
            url += t.getDatum();
            url += "/";
            url += per;
            counteruurmax = 11;
        } else if (loc.contains("herenlaan")) {
            url += Constants.RoosterHerenlaan;
            url += per;
            counteruurmax = 13;
        } else if (loc.contains("beukenlaan")) {
            url += Constants.RoosterBeukenlaan;
            url += per;
            counteruurmax = 11;
        } else {
            return false;
        }

        String htmlcode = t.getHtml(url);
        TagNode html = hc.clean(htmlcode);
        Document doc = new DomSerializer(cp, true).createDOM(html);
        XPath xp = XPathFactory.newInstance().newXPath();
        NodeList nl = (NodeList) xp.evaluate(Constants.XPathRooster, doc, XPathConstants.NODESET);

        int counteruur = 1;
        int counterdag = 1;
        int decreaser  = 0;
        Boolean isPauze = false;
        RoosterItems RItems = new RoosterItems();
        RoosterItem  RItem  = null;
        for (int i = 0; i < nl.getLength(); i++){

            if ((counteruur == pauze[0]) || (counteruur == pauze[1]) || (counteruur == pauze[2])) {
                isPauze = true;
                decreaser++;
            }

            if (!isPauze) {
                RItem = new RoosterItem();
                switch (counterdag){
                case 1:
                    RItem.setDag("ma");
                    break;
                case 2:
                    RItem.setDag("di");
                    break;
                case 3:
                    RItem.setDag("wo");
                    break;
                case 4:
                    RItem.setDag("do");
                    break;
                case 5:
                    RItem.setDag("vr");
                    break;
                }

                Node n = nl.item(i);
                String content = n.getTextContent();
                if (content.length() > 1) {
                    RItem.setUur(""+(counteruur-decreaser));
                    NodeList t1 = n.getChildNodes();
                    NodeList t2 = t1.item(0).getChildNodes();
                    NodeList t3 = t2.item(0).getChildNodes();
                    for (int j = 0; j < t3.getLength(); j++) {
                        Node temp = t3.item(j);
                        if (t3.getLength() == 3) {
                            switch (j) {
                            case 0:
                                RItem.setLes(""+temp.getTextContent());
                                break;
                            case 1:
                                RItem.setLokaal(""+temp.getTextContent());
                                break;
                            case 2:
                                RItem.setDocent(""+temp.getTextContent());
                                break;
                            default:
                                return false;
                            }
                        } else if (t3.getLength() == 4) {
                            switch (j) {
                            case 0:
                                break;
                            case 1:
                                RItem.setLes("tts. " + temp.getTextContent());
                                break;
                            case 2:
                                RItem.setLokaal(""+temp.getTextContent());
                                break;
                            case 3:
                                RItem.setDocent(""+temp.getTextContent());
                                break;
                            default:
                                return false;
                            }
                        } else if (t3.getLength() == 1) {
                            RItem.setLes(""+temp.getTextContent());
                        } else {
                            return false;
                        }
                    }
                } else {
                    RItem.setUur("" + (counteruur-decreaser));
                    RItem.setLokaal("Vrij");
                }
                RItems.add(RItem);
            }
            if (counteruur == counteruurmax) { counteruur = 0; counterdag++; decreaser = 0;}
            counteruur++;
            isPauze = false;
        }

        if (RItems.size() > 0) {
            mSQL = new RoosterSQLAdapter(mContext);
            mSQL.openToWrite();
            mSQL.deleteAll();
            for (int j = 0; j < RItems.size(); j++) {
                RoosterItem insert = RItems.get(j);
                mSQL.insert(insert.getDag(), insert.getUur(), insert.getLes(), insert.getLokaal(), insert.getDocent());
            }
            if (mSQL != null) mSQL.close();
        }
        return true;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return false;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        return false;
    }

有几个常数,但我认为你可以自己猜到他们;),否则你知道怎么问我他们:)

There are a few constants but I think you can guess them yourself;) and otherwise you know how to ask me for them:)

该RoosterItem类将举行一个小时的所有变量,而RoosterItems将举行多个RoosterItem

The RoosterItem class will hold all variables of an hour, and the RoosterItems will hold more than one RoosterItem

祝您好运!

这篇关于Android的:如何解析HTML表成的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆