The example in this article describes how Android uses Jsoup to parse HTML tables. Share to everyone for your reference, specific as follows:
Look at the code, you can parse the table in the Label text button to add to their needs, hehe
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 string to parse";
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) {///Loop table row TR object TableRow TableRow = new TableRow (this);
Elements cols = Row.children ();
for (Element col:cols) {//loop row of the column TD object 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 bu
Tton = 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 OnC
Lick}}); }//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 ()}
I hope this article will help you with your Android programming.