Android使用Jsoup解析Html表格的方法
程序员文章站
2023-12-19 20:47:52
本文实例讲述了android使用jsoup解析html表格的方法。分享给大家供大家参考,具体如下:
看代码吧,可解析表中的label text button 自己根据需要...
本文实例讲述了android使用jsoup解析html表格的方法。分享给大家供大家参考,具体如下:
看代码吧,可解析表中的label text button 自己根据需要再添加,呵呵
import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.nodes.element; import org.jsoup.select.elements; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.view.window; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.textview; import android.widget.toast; public class tableparseactivity extends activity{ private document doc; private string html = null; private tablelayout tablelayout; private final int wc = viewgroup.layoutparams.wrap_content; private final int fp = viewgroup.layoutparams.fill_parent; private final int width = 80; private string functionname,fields; private list<namevaluepair> params; private static string url; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.analyzing); html = "需要解析的html字符串"; tableparse(); } public void tableparse(){ doc = jsoup.parse(html); elements trs = doc.select("tr"); tablelayout = (tablelayout)findviewbyid(r.id.tablelayout1); tablelayout.layoutparams p = new tablelayout.layoutparams(fp, wc); this.settitle(doc.title()); for (element row : trs) {//循环表下的行 tr对象 tablerow tablerow = new tablerow(this); elements cols = row.children(); for (element col : cols) {//循环行下的列 td对象 elements children = col.children(); for (element child : children) { if(child.tagname().equals("label")){ textview textview = new textview(this); textview.settext(child.val()); textview.settextcolor(color.black); tablerow.addview(textview); }else if(child.tagname().equals("input")&&child.attributes().get("type").equals("text")){ edittext edittext = new edittext(this); edittext.settext(child.val()); edittext.setwidth(width); tablerow.addview(edittext); string id = child.attributes().get("id"); if(id.length() > 0){ edittext.setid(integer.parseint(child.attributes().get("id"))); } }else if(child.tagname().equals("input")&&child.attributes().get("type").equals("button")){ button button = new button(this); button.settext(child.val()); tablerow.addview(button); fields = child.attributes().get("fields"); functionname = child.attributes().get("functionname"); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { //todo onclick } }); }//end if(child.tagname().equals("input")&&child.attributes().get("type").equals("button")) }//end for (element child : children) }//end for (element col : cols) tablelayout.addview(tablerow,p); }//end for (element row : rows) }//end tableparse() }
希望本文所述对大家android程序设计有所帮助。