How Android uses Jsoup to parse HTML tables _android

Source: Internet
Author: User
Tags tagname

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.