Ebad khan Ebad khan - 6 months ago 32
Java Question

Want to get the Title and Grade using Jsoup

I want to get the Title and Grade of the subject from a table using Jsoup. I have Successfully got the Title of the subject, but not getting the grade. I am getting the grade plus the Subject code of the subject, but I just want the grade. It will be really helpful if you guys help me solve the problem.

For Example from the first row of this table I want Operating Systems and grade B+ and so on.

Here is the table..

<table class="FullWidth gv" cellspacing="0" align="Center" rules="all" border="1" id="ctl00_Body_gvResult" style="border-collapse:collapse;">
<tr>
<th class="Center" scope="col">S#</th><th class="Center" scope="col">Code</th><th class="Center" scope="col">Title</th><th class="Center" scope="col">Grade</th>
</tr><tr>
<td class="Center">
1
</td><td class="Center NoWrap">CSC 330</td><td class="Left FullWidth">Operating Systems</td><td class="Center NoWrap">B+</td>
</tr><tr class="Alternating">
<td class="Center">
2
</td><td class="Center NoWrap">CSL 330</td><td class="Left FullWidth">Operating System Lab</td><td class="Center NoWrap">B+</td>
</tr><tr>
<td class="Center">
3
</td><td class="Center NoWrap">GSC-205</td><td class="Left FullWidth">Complex Variables &amp; Transforms</td><td class="Center NoWrap">A</td>
</tr><tr class="Alternating">
<td class="Center">
4
</td><td class="Center NoWrap">SEL-206</td><td class="Left FullWidth">Database Management Systems Lab</td><td class="Center NoWrap">A</td>
</tr><tr>
<td class="Center">
5
</td><td class="Center NoWrap">SEL-217</td><td class="Left FullWidth">Data Structures &amp; Algorithms Lab</td><td class="Center NoWrap">A</td>
</tr><tr class="Alternating">
<td class="Center">
6
</td><td class="Center NoWrap">SEN-206</td><td class="Left FullWidth">Database Management Systems</td><td class="Center NoWrap">A</td>
</tr><tr>
<td class="Center">
7
</td><td class="Center NoWrap">SEN-217</td><td class="Left FullWidth">Data Structures &amp; Algorithms</td><td class="Center NoWrap">B</td>
</tr>
</table>


Result.java

package com.example.ebad.apppw;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;

public class Result extends AppCompatActivity {

String urlws = "http://111.68.99.8/StudentProfile/Result.aspx";
HashMap<String, String> hashMaps;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_layout);

hashMaps = Data.map;

new results().execute();


}


private class results extends AsyncTask<Void, Void, Void> {

String data;
String data2;

@Override
protected void onPreExecute() {
//super.onPreExecute();

}

@Override
protected Void doInBackground(Void... params) {

try {
Document doces = Jsoup.connect(urlws)
.cookies(hashMaps)
.get();


Elements elements = doces.select("table#ctl00_Body_gvResult").first().getElementsByTag("td");

for (Element element : elements) {
if (element.className().contains("Left")) {
data = element.ownText();
System.out.println(data);
}
}

for (Element element : elements) {
if (element.className().contains("Center")) {
if (element.className().endsWith("NoWrap")) {

data2 = element.ownText();
System.out.println(data2);

}
}
}



} catch (Exception e) {
e.printStackTrace();
}

return null;
}


@Override
protected void onPostExecute(Void aVoid) {
super.onPreExecute();

}


}


}

Answer

Try this:

Document doc = Jsoup.parse(html,"");

Element tableEl = doc.select("table#ctl00_Body_gvResult").first();
Elements rowEls = tableEl.select("tr");

for (Element rowEl : rowEls){
    Elements tds = rowEl.select("td");
    if (tds.isEmpty() || tds.size()<4){
        continue;
    }
    //title
    String title = tds.get(2).ownText();

    //grade
    String grade = tds.get(3).ownText();

    System.out.println("title: "+title+", grade: "+grade);
}

This assumes of course that in the table you always find 4 columns of which column 3 holds the course title and column 4 holds the grade. The check for tds.isEmpty() || tds.size()<4 is needed because the first row of the table holds the headers.