Create Table from HTML code - android

<table style="width: 560px; border: 2px solid #fee3cc; font-size: 1em;" rules="all" border="1" cellpadding="5" cellspacing="0">
<tbody>
<tr>
<td>
<p>Bharatiya Jana Sangh</p>
</td>
<td>
<p>1951 - 1977</p>
</td>
</tr>
<tr>
<td>
<p>Janata Party</p>
</td>
<td>
<p>1977 - 1979</p>
</td>
</tr>
<tr>
<td>
<p>Bharatiya Janata Party</p>
</td>
<td>
<p>1980</p>
</td>
</tr>
</tbody>
</table>
I have the html code of table as above, and I want to directly show a new table on layout as above table code, how I can do this

In your activity:
WebView webview = new WebView(this);
setContentView(webview);
String yourHtml = "<html><body><table>...</table></body></html>";
webview.loadData(yourHtml , "text/html", "utf-8");

Related

Parse specific table to array of strings Jsoup

i have this, rather complex html I want to parse using JSoup. I have tried several things, but none is working. Basically, I wanted to get the second table, and read all rows and append it to string.
What I have tried
val document = Jsoup.parse(it.data)
val tableElements = document.select("table:eq(2) > tbody")
for (element in tableElements) {
val data = element.select("td")
try {
Timber.i("${data[0].select("small").text()} : ${data[1].select("small").text()}")
} catch (e: Exception) {
}
}
What part I want to extract
<table>
<tbody>
<tr class="">
<td class="odsazena" align="left"><small>User's identification number: </small></td>
<td class="odsazena" align="left"><small>34565</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Study programme: </small></td>
<td class="odsazena" align="left"><small>Informatics</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Type of study: </small></td>
<td class="odsazena" align="left"><small>Bachelor</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Form of study: </small></td>
<td class="odsazena" align="left"><small>full-time, attendance method</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Standard length of study: </small></td>
<td class="odsazena" align="left"><small>3</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Number of credits required to complete your study: </small></td>
<td class="odsazena" align="left"><small>180</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Number of credits enrolled for the whole study: </small></td>
<td class="odsazena" align="left"><small>120</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Number of credits obtained during your whole course of study: </small></td>
<td class="odsazena" align="left"><small>90</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Your prospective academic degree: </small></td>
<td class="odsazena" align="left"><small>Bc.</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Beginning of study: </small></td>
<td class="odsazena" align="left"><small>09/01/2017</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Resolution of admission: </small></td>
<td class="odsazena" align="left"><small>Admitted without the entrance exam</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Progress of study: </small></td>
<td class="odsazena" align="left"><small>enrolled</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Mode of completion: </small></td>
<td class="odsazena" align="left"><small><i>not stated</i></small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Current financing: </small></td>
<td class="odsazena" align="left"><small>study fully financed from ME SK</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Final thesis topic: </small></td>
<td class="odsazena" align="left"><small><i>not stated</i></small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Final thesis supervisor: </small></td>
<td class="odsazena" align="left"><small><i>not stated</i></small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Language of study: </small></td>
<td class="odsazena" align="left"><small>Slovak</small></td>
</tr>
<tr class="">
<td class="odsazena" align="left"><small>Card number:</small></td>
<td class="odsazena" align="left"><small>123456</small></td>
</tr>
</tbody>
</table>
And now, what is the problem exactly? Well from what I've tried, the code does not even let me print the stuff I want, and in the current state as it is it will just skip the for cycle. What I wanted to achieve is that I wanted to get to second table "table:eq(2)" and get elements inside "tbody"
I think that you should select the "tr" elements as well and iterate over them as looks like you are iteratin over "tbody". This is a solution in Java, as I don't know Kotlin syntax, but maybe it helps:
Elements tableElements = doc.select("table").get(1).select("tbody").select("tr");
for (Element element : tableElements) {
Elements data = element.select("td");
System.out.println(data.select("small").first().text() +" : "
+ data.select("small").last().text());
}
This is java code to do what you want.
You can apply selector on elements.
#Test
public void selectSecondTable() {
String html = "" +
"<table></table>" +
"<table>\n" +
" <tbody>\n" +
" <tr class=\"\">\n" +
" <td class=\"odsazena\" align=\"left\"><small>User's identification number: </small></td>\n" +
" <td class=\"odsazena\" align=\"left\"><small>34565</small></td>\n" +
" </tr>\n" +
" </tbody>\n" +
"</table>";
Document doc = Jsoup.parse(html);
//select tr from second table in document:
for (Element e : doc.select("table:eq(1) tr")) {
//for each table row select text from small tag and print to console:
System.out.println(e.select("small").text());
}
}

Want to get href and title from the table using Jsoup

I want to parse Html table using Jsoup but I am having trouble getting my requried data from it. I want to get href and title from each row of this table but I am getting the whole data from the table.
<table class="FullWidth gv" cellspacing="0" rules="all" border="1" id="ctl00_Body_STUDENT_SSS_ctrl0_COURSE_REGISTRATION" style="border-collapse:collapse;">
<tr>
<th scope="col">S#</th>
<th scope="col">Code</th>
<th scope="col">Registered Course Title</th>
<th scope="col">Credits</th>
<th scope="col">Offered Course Title</th>
<th scope="col">Class</th>
<th scope="col">Teacher</th>
<th scope="col">Fee</th>
<th scope="col"> </th>
</tr>
<tr>
<td class="Center">
1</td>
<td class="NoWrap">GSC 220</td>
<td class="Width33">Complex Variables & Transforms</td>
<td class="Center">3</td>
<td class="Width33">Complex Variables & Transforms</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">AMMAR AJMAL</td>
<td>YES</td>
<td>
<a title="Complex Variables & Transforms" class="a" href="Attendance.aspx?COID=21480" target="_blank">Attendance</a>
</td>
</tr>
<tr class="Alternating">
<td class="Center">
2</td>
<td class="NoWrap">CSC 221</td>
<td class="Width33">Data Structure and Algorithm</td>
<td class="Center">3</td>
<td class="Width33">Data Structure and Algorithm</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">ABU BAKAR</td>
<td>YES</td>
<td>
<a title="Data Structure and Algorithm" class="a" href="Attendance.aspx?COID=21478" target="_blank">Attendance</a>
</td>
</tr>
<tr>
<td class="Center">
3</td>
<td class="NoWrap">CSL 221</td>
<td class="Width33">Data Structures and Algorithm Lab</td>
<td class="Center">1</td>
<td class="Width33">Data Structures and Algorithm Lab</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">ABU BAKAR</td>
<td>YES</td>
<td>
<a title="Data Structures and Algorithm Lab" class="a" href="Attendance.aspx?COID=21479" target="_blank">Attendance</a>
</td>
</tr>
<tr class="Alternating">
<td class="Center">
4</td>
<td class="NoWrap">CSC 220</td>
<td class="Width33">Database Management System</td>
<td class="Center">3</td>
<td class="Width33">Database Management System</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">BUSHRA SABIR</td>
<td>YES</td>
<td>
<a title="Database Management System" class="a" href="Attendance.aspx?COID=21481" target="_blank">Attendance</a>
</td>
</tr>
<tr>
<td class="Center">
5</td>
<td class="NoWrap">CSL 220</td>
<td class="Width33">Database Management System Lab</td>
<td class="Center">1</td>
<td class="Width33">Database Management System Lab</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">BUSHRA SABIR</td>
<td>YES</td>
<td>
<a title="Database Management System Lab" class="a" href="Attendance.aspx?COID=21482" target="_blank">Attendance</a>
</td>
</tr>
<tr class="Alternating">
<td class="Center">
6</td>
<td class="NoWrap">CSC 320</td>
<td class="Width33">Operating System</td>
<td class="Center">3</td>
<td class="Width33">Operating System</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">BUSHRA SABIR</td>
<td>YES</td>
<td>
<a title="Operating System" class="a" href="Attendance.aspx?COID=21474" target="_blank">Attendance</a>
</td>
</tr>
<tr>
<td class="Center">
7</td>
<td class="NoWrap">CSL 320</td>
<td class="Width33">Operating System Lab</td>
<td class="Center">1</td>
<td class="Width33">Operating System Lab</td>
<td class="NoWrap">BCE-4 (A) MORNING</td>
<td class="Width33">BUSHRA SABIR</td>
<td>YES</td>
<td>
<a title="Operating System Lab" class="a" href="Attendance.aspx?COID=21475" target="_blank">Attendance</a>
</td>
</tr>
<tr class="gvFooter">
<td> </td>
<td> </td>
<td> </td>
<td class="Center">15</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
I am trying like this
Document doce = Jsoup.connect(urlofthewebsite)
.cookies(hashMap)
.get();
Element tableheader = doce.select("table[id=ctl00_Body_STUDENT_SSS_ctrl0_COURSE_REGISTRATION}").first();
for(Element element : tableheader.children())
{
System.out.println(element.text());
}
First of all, your example have typo at
select("table[id=ctl00_Body_STUDENT_SSS_ctrl0_COURSE_REGISTRATION}")
since you are ending attribute selector with } instead of ].
You avoid such errors with id start using #identifier instead of [id=identifier] and .className instead of [class=className].
Also by calling
.select("table[id=ctl00_Body_STUDENT_SSS_ctrl0_COURSE_REGISTRATION]")
.first();
you are not getting first row from table (like headers), but first table with this id (since such elements - tables with specific id - your selector suppose to find).
If you want find headers simply pick them by selecting th tags like
Element table = doce.select("table#ctl00_Body_STUDENT_SSS_ctrl0_COURSE_REGISTRATION").first();
for(Element column : table.select("th")) {
System.out.println(column.text());
}
Now based on
I want to get href and title from each row of this table but I am getting the whole data from the table.
you may want to use something like
for (Element link : table.select("a")){
System.out.println(link.attr("title")+" -> "+link.attr("href"));
//you can also use abs:href to get absolute path
}

How to login by filling a form in a website using Android

I'm trying to log on to a website that has a form to which you should provide user-name and password, check a box, and press a login button. I tried all kinds of httpClient POST messages, but it seems that it is not working. Can anyone assist and point to an example of skeleton of android Java way to login? Here is the form from the html page:
<form name="loginForm" method="post" action="/login.do">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10px"> </td>
<td><label class="formLabel" for="loginID">Username</label></td>
</tr>
<tr>
<td> </td>
<td><input type="text" name="username" value="" class="formTextField"></td>
</tr>
<tr>
<td> </td>
<td><label class="formLabel" for="password"> Password</label></td>
</tr>
<tr>
<td> </td>
<td><input type="password" name="password" value="" class="formTextField"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="checkbox" name="agreement" value="on" class="formTextField">
I agree with <div>
<b>Terms and Conditions</b></div>
</td>
</tr>
</table>
<p><input type="submit" value="Login" class="FPFormFieldB"></p>
<p>Have you forgotten the password?</p>
<p>New user registration</p>
</form>
I managed to login using JSOUP.
The key is that you need to do a get first, then a post, with the cokies (that includes SessionID and other stuff).
Here is the code that worked for me, hopefully it will assist others:
import android.provider.DocumentsContract;
import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Webbing {
public static void Open() throws Exception {
Connection.Response loginForm = Jsoup.connect("http://your website")
.method(Connection.Method.GET)
.execute();
Document document = Jsoup.connect("http://your website")
.data("username", "XXX")
.data("password", "YYY")
.data("agreement", "on")
.timeout(5000)
.cookies(loginForm.cookies())
.post();
String url = "http://a page you want to load after login";
Document fpl = Jsoup.connect(url)
.timeout(5000)
.cookies(loginForm.cookies())
.get();
body = fpl.body().toString();
ExtFile.write(body);
}
}

Android parse html table

I need to parse data table look like this:
<table width="75%" border="2" id="INVALSI">
<tbody>
<tr>
<td width="5%" align="center"><strong>UNITA'<br>SINTATTICA
</strong></td>
<td width="45%" align="center"><strong>ANALISI<br>LOGICA
</strong></td>
<td width="50%" align="center"><strong>RISPONDE<br>ALLA
DOMANDA:
</strong></td>
</tr>
<tr>
<td align="left" style="padding: 4px 4px 2px 6px;"><strong>ciao</strong></td>
<td align="left" style="padding: 4px 4px 2px 6px;"><strong>complemento
vocativo (o esclamativo)</strong></td>
<td align="left" style="padding: 4px 4px 2px 6px;"><strong>CI
SI INDIRIZZA A QUALCUNO?</strong><br></td>
</tr>
</tbody>
</table>
How can I select the "strong" and parse it into a listview?
You ca use Jsoup HTML parser to get the required data
Step 1. Download the jsoup.jar from http://jsoup.org/download
Step 2. Add it to the libs folder of your project.
Step 3. Extract Data required
ArrayList<String> list = new ArrayList<String>();
Document doc = Jsoup.parse("your html");
Elements elements = doc.select("strong");
for(int i=0;i<elements.size();i++)
{
list.add(elements.get(i).text().toString());
}
Finally
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
As CommonsWare sayed in this post
There are many HTML parsers : http://java-source.net/open-source/html-parsers

Html onclick firing twice in Android

Code:
function hello(){alert("Hi");};
<center>
<table border="0" height="100%">
<tbody>
<tr>
<td align="center" width="100%">
<img src="cover.png"
width="300" height="300" id="image"></img></td>
</tr>
<tr>
<td height="100%"> </td>
</tr>
<tr>
<td align="center">
<form onsubmit="go();return false">
<input class="answer" id="answer" name="answer"
onclick="hello();"/>
</form>
</td>
</tr>
<tr>
</tr>
</tbody>
</table>
</center>
I have been trying to capture when the answer text box is clicked in HTML. It works perfectly in Firefox and Chrome (I only get one Hi alert), but the onclick method fires twice when I try to run the code in a web-view in android (I get two Hi alerts). However, when I call the same function later it works properly, firing only one Hi alert.
<div style="bottom: 0; right: 0; position:absolute; margin-right:5%">
<a><img alt="" src="start.png" id="submit" onclick="go();hello();"></a>
</div>
I'm guessing it has something to do with the fact that I'm calling the function from inside the form and it's somehow firing the event twice but I have no idea how to fix it. Any help?
You could try using one() as follows.
$('#answer').one('click', function() {
alert("Hi");
});
<form onsubmit="go();return false">
<input class="answer" id="answer" name="answer"/>
</form>

Categories

Resources