Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 用 Jsoup 解析 HTML 表格並動態產生表單

編輯:關於 Android 程式設計

本文示範如何以 Jsoup 解析一段 HTML 表格字串,並把其中的 label、text 輸入框、button 等元素對應轉成 Android 的原生控制項,動態組出一個 TableLayout 表單。

TableParseActivity

 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() } 

解析思路

整個流程是三段巢狀迴圈:doc.select("tr") 取出所有表格列,接著用 row.children() 取得該列的儲存格,最後再逐一檢查儲存格內的子元素標籤名稱。

對應關係很單純:label 轉成 TextView、type="text" 的 input 轉成 EditText(並可透過 id 屬性設定控制項 ID)、type="button" 的 input 轉成 Button 並掛上點擊監聽。原本 HTML 上的自訂屬性(例如 fields、functionName)也能用 child.attributes().get(...) 讀出來當作後續邏輯的依據。

想再擴充 select、textarea 或 checkbox 這類元素,只要照同樣模式加分支即可。

希望本文所述對大家 Android 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved