Skip to content Skip to sidebar Skip to footer

Android: How To Parse Html Table Into Listview?

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? Thanks in

Solution 1:

You will first need to parse the HTML table into a data structure, and then use ListView to display that information. Try using the JSoup library to do the HTML parsing: http://jsoup.org/cookbook/introduction/parsing-a-document

Solution 2:

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)

Im using HTML Cleaner library for parsing the html:

try {   
        HtmlCleanerhc=newHtmlCleaner();
        CleanerPropertiescp= hc.getProperties();
        cp.setAllowHtmlInsideAttributes(true);
        cp.setAllowMultiWordAttributes(true);
        cp.setRecognizeUnicodeChars(true);
        cp.setOmitComments(true);

        Stringloc= sp.getString( Constants.pref_locatie      , "" );
        Stringper= sp.getString( Constants.pref_persoon      , "" );
        Stringoob= sp.getString( Constants.pref_onderofboven , "" );

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

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

        Stringhtmlcode= t.getHtml(url);
        TagNodehtml= hc.clean(htmlcode);
        Documentdoc=newDomSerializer(cp, true).createDOM(html);
        XPathxp= XPathFactory.newInstance().newXPath();
        NodeListnl= (NodeList) xp.evaluate(Constants.XPathRooster, doc, XPathConstants.NODESET);

        intcounteruur=1;
        intcounterdag=1;
        intdecreaser=0;
        BooleanisPauze=false;
        RoosterItemsRItems=newRoosterItems();
        RoosterItemRItem=null;
        for (inti=0; i < nl.getLength(); i++){

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

            if (!isPauze) {
                RItem = newRoosterItem();
                switch (counterdag){
                case1:
                    RItem.setDag("ma");
                    break;
                case2:
                    RItem.setDag("di");
                    break;
                case3:
                    RItem.setDag("wo");
                    break;
                case4:
                    RItem.setDag("do");
                    break;
                case5:
                    RItem.setDag("vr");
                    break;
                }

                Noden= nl.item(i);
                Stringcontent= n.getTextContent();
                if (content.length() > 1) {
                    RItem.setUur(""+(counteruur-decreaser));
                    NodeListt1= n.getChildNodes();
                    NodeListt2= t1.item(0).getChildNodes();
                    NodeListt3= t2.item(0).getChildNodes();
                    for (intj=0; j < t3.getLength(); j++) {
                        Nodetemp= t3.item(j);
                        if (t3.getLength() == 3) {
                            switch (j) {
                            case0:
                                RItem.setLes(""+temp.getTextContent());
                                break;
                            case1:
                                RItem.setLokaal(""+temp.getTextContent());
                                break;
                            case2:
                                RItem.setDocent(""+temp.getTextContent());
                                break;
                            default:
                                returnfalse;
                            }
                        } elseif (t3.getLength() == 4) {
                            switch (j) {
                            case0:
                                break;
                            case1:
                                RItem.setLes("tts. " + temp.getTextContent());
                                break;
                            case2:
                                RItem.setLokaal(""+temp.getTextContent());
                                break;
                            case3:
                                RItem.setDocent(""+temp.getTextContent());
                                break;
                            default:
                                returnfalse;
                            }
                        } elseif (t3.getLength() == 1) {
                            RItem.setLes(""+temp.getTextContent());
                        } else {
                            returnfalse;
                        }
                    }
                } 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 = newRoosterSQLAdapter(mContext);
            mSQL.openToWrite();
            mSQL.deleteAll();
            for (intj=0; j < RItems.size(); j++) {
                RoosterIteminsert= RItems.get(j);
                mSQL.insert(insert.getDag(), insert.getUur(), insert.getLes(), insert.getLokaal(), insert.getDocent());
            }
            if (mSQL != null) mSQL.close();
        }
        returntrue;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        returnfalse;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        returnfalse;
    }

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

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

Good Luck!

Solution 3:

So far i think JSoup is one of the best way to extract or manipulate the HTML.....

See this link :

http://jsoup.org/

But somehow.... this did't worked in my case, so i converted the entire HTML code into String, then parsed it.....

Post a Comment for "Android: How To Parse Html Table Into Listview?"