Android java HTML Table 到 Hashmap
Android java HTML Table to Hashmap
我是 java 的新人。我想读取 html table 并将数据推送到哈希图中:
输出应该是:
row1: line1, line2, line3
row2: line1, line2, line3
该文件仅包含一个 table 带有 td 和 tr 标签且格式正确。
怎么做?
PS:有人想把我的 python 代码翻译成 java 吗? ;)
我会尝试用简单的方式描述过程:
1) 每行都是 java 对象,例如:
public class Client {
String name;
int age;
...
}
2) 可以把Client放在Hashmap中
Map <Integer, Client> myClients = new HashMap<Integer, Client>();
myClients.put(1, new Client())
3) 您可以将 Client 对象连接到布局
item.findViewById(R.id.client_name);
x.setText("Name is " + name);
您可以使用 jsoup:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class TableEg extends Activity{
Hashmap<Integer, ArrayList<String>> hashMap = new Hashmap<Integer, ArrayList<String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int count = 0;
String html = "<table><tr><td>1</td><td>Some Value</td><td>Some Other Value</td><td>Yet another value</td></tr></table>";
try {
Document doc = Jsoup.parse(html);
Elements tableElements = doc.select("table");
Elements tableRowElements = tableElements.select(":not(thead) tr");
for (int i = 0; i < tableRowElements.size(); i++) {
Element row = tableRowElements.get(i);
ArrayList<String> arrayList = new ArrayList<String>();
Elements rowItems = row.select("td");
for (int j = 0; j < rowItems.size(); j++) {
arrayList.add(rowItems.get(j).text());
}
hashMap.put(Integer.valueOf(count), arrayList);
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我是 java 的新人。我想读取 html table 并将数据推送到哈希图中:
输出应该是:
row1: line1, line2, line3
row2: line1, line2, line3
该文件仅包含一个 table 带有 td 和 tr 标签且格式正确。
怎么做?
PS:有人想把我的 python 代码翻译成 java 吗? ;)
我会尝试用简单的方式描述过程:
1) 每行都是 java 对象,例如:
public class Client {
String name;
int age;
...
}
2) 可以把Client放在Hashmap中
Map <Integer, Client> myClients = new HashMap<Integer, Client>();
myClients.put(1, new Client())
3) 您可以将 Client 对象连接到布局
item.findViewById(R.id.client_name);
x.setText("Name is " + name);
您可以使用 jsoup:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class TableEg extends Activity{
Hashmap<Integer, ArrayList<String>> hashMap = new Hashmap<Integer, ArrayList<String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int count = 0;
String html = "<table><tr><td>1</td><td>Some Value</td><td>Some Other Value</td><td>Yet another value</td></tr></table>";
try {
Document doc = Jsoup.parse(html);
Elements tableElements = doc.select("table");
Elements tableRowElements = tableElements.select(":not(thead) tr");
for (int i = 0; i < tableRowElements.size(); i++) {
Element row = tableRowElements.get(i);
ArrayList<String> arrayList = new ArrayList<String>();
Elements rowItems = row.select("td");
for (int j = 0; j < rowItems.size(); j++) {
arrayList.add(rowItems.get(j).text());
}
hashMap.put(Integer.valueOf(count), arrayList);
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}