I am new in Android programming. I need to get values from HTML and display it in list.
Here is link http://www.hak.hr/info/cijene-goriva/
->so I need values (10,41,10.51)
<div id="div_eurosuper95">
<table class="nowrapper fuel_segmented">
<thead>
<tr>
<th>
Gorivo
</th>
<th>
Cijena (kn)
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="fuel_name"><span class="vendorName">Tifon</span></br>euroSUPER 95 BS</td>
<td class="fuel_segmented">10,41</td>
</tr>
<tr>
<td class="fuel_name"><span class="vendorName">Tifon</span></br>EUROSUPER 95
BS CLASS</td>
<td class="fuel_segmented">10,51</td>
</tr>
<tr>
<td class="fuel_name"><span class="vendorName">Crodux derivati</span></br>EUROSUPER 95 BS</td>
<td class="fuel_segmented">10,41</td>
</tr>
<tr>
<td class="fuel_name"><span class="vendorName">AdriaOil</span></br>Euro Super 95 BS TOP</td>
<td class="fuel_segmented">10,51</td>
</tr>
</tbody>
</table>
</div>
You can use Jsoup selector to select all the <td> tags that have are of class fuel_segmented.
Document doc = Jsoup.parse(html);
Elements fuel = doc.select("td.fuel_segmented");
This is a basic CSS selector syntax, where the td specifies the tag, and the . specifies that it is a class. If it was a specific td with an id you could've specified it as td#fuel_segmented.
This will return a collection of Element objects, represented by an Elements object.
To make it a bit more easy to see what is what, you can loop through the elements and display the corresponding fuel name.
Elements fuel = doc.select("td.fuel_segmented");
for (Element element : fuel) {
System.out.println(element.previousElementSibling().text()
+ ": " + element.text());
}
which will output
Tifon euroSUPER 95 BS: 10,41
Tifon EUROSUPER 95 BS CLASS: 10,51
Crodux derivati EUROSUPER 95 BS: 10,41
AdriaOil Euro Super 95 BS TOP: 10,51
I suggest that you read more about how to use the selector in Jsoup to parse the data that you need. That part of the cookbook can be found here.
To display your datas in ListView, there is a good tutorial to understand how does it work.
I really don't know where did you get these prices, on Jsoup you have all the "Cookbooks" necessary, with examples to parse html document.
Related
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 save editable content loaded of local storage?
myWebView.loadUrl("file:///android_asset/www/table.html")
<table id="customers">
<tbody><tr class="alt"><th> </th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
<tr class="alt"><td>1</td>
<td contenteditable='true'>asdasd</td>
<td contenteditable='true'>153213 </td>
<td contenteditable='true'>253625213</td>
<td contenteditable='true'>026426</td>
<td contenteditable='true' >asdasd</td>
</tr>
You can do this by first injecting a JavaScript-accessible POJO using WebView.addJavascriptInterface(). Your Android POJO would have the code to save the data. Then set up a JavaScript event listener to fire the POJO method. You could listen for a Done button click from the user or perhaps the new HTML5 input event.
please help me parse my special table html which contains many rows
I try this code : Elements table : doc.select(".titre2_tableau); It give list of all elements like :
David
13:30
DELL
Prof
13:55
SAM
14:15
HP
PROD
14:09
peter
13 :50
ACER
Beginner
14:12
But i need this list divided of group of 5 items like :
David - 13:3-0 – DELL – Prof - 13:55
SAM - 14:15 – HP – PROD - 14:09
I think need a loop and iterator but i can't apply them for my case . Thanks for help
<tr align="center">
<td class="titre2_tableau" bgcolor="#F1FBFF">David</td>
<td class="titre2_tableau" bgcolor="#F1FBFF">13:30</td>
<td class="titre2_tableau" bgcolor="#F1FBFF">DELL</td>
<td class="titre2_tableau" bgcolor="#F1FBFF">Prof</td>
<td class="titre2_tableau" bgcolor="#F1FBFF"> 13:55</td>
</tr>
<tr align="center">
<td class="titre2_tableau" bgcolor="#e0f4fd">SAM</td>
<td class="titre2_tableau" bgcolor="#e0f4fd">14:15</td>
<td class="titre2_tableau" bgcolor="#e0f4fd">HP</td>
<td class="titre2_tableau" bgcolor="#e0f4fd">PROD</td>
<td class="titre2_tableau" bgcolor="#e0f4fd"> 14:09</td>
</tr>
Try the following code:
final Elements tableElements = doc.select("tr");
for (Element element : tableElements) {
String item = element.getElementsByClass("titre2_tableau").text();
}
The tableElements variable will contain each row of data (tr) while the for loop will grab each table data (td) from the corresponding row. You will have to play around with the loop to get exactly what you would like but the above code should get you started.
How can I get bolded element "THIS" from HTML using jsoup. My problem is that I don't know how to get to this element, because I need to detect if its from <tr> "Ulica" first. what do I need to put in document.select(...)? Any ideas? Thanks.
<table class="InfoTable">
<tr>
<td class="Name">Ulica:</td>
<td class="Value"><span id="ctl00_RightContentPlaceholder_lbAregStreet">**THIS**</span></td>
</tr>
<tr>
<td class="Name">Mesto:</td>
<td class="Value"><span id="ctl00_RightContentPlaceholder_lbAregCity">XXXXX</span></td>
</tr>
<tr>
<td class="Name">PSČ:</td>
<td class="Value"><span id="ctl00_RightContentPlaceholder_lbAregZip">XXXX</span></td>
</tr>
<tr>
<td class="Name">Štát:</td>
<td class="Value"><span id="ctl00_RightContentPlaceholder_lbAregCountry">XXXXX</span></td>
</tr>
</table>
You can put all that into a single selector
Example:
// html is your posted html code here, you can connect to a website too.
final String html = ...
Document doc = Jsoup.parse(html); // Parse into document
// Select the element and print it
for( Element element : doc.select("td:contains(Ulica:) ~ td") )
{
System.out.println(element);
}
Explanation:
td:contains(Ulica:) ~ td: Selects td elements with text Ulicia, and takes the next sibling element that's a td.
Output:
<td class="Value"><span id="ctl00_RightContentPlaceholder_lbAregStreet">**THIS**</span></td>
Now you can get the values you need form that element.
Take a look at this; it is a nice way to do HTML parsing in Java.
String html="<table class=\"InfoTable\"<tr><td class=\"Name\">Ulica:</td> <td class=\"Value\"><span id=\"ctl00_RightContentPlaceholder_lbAregStreet\">**THIS**</span></td></tr><tr><td class=\"Name\">Mesto:</td><td class=\"Value\"><span id=\"ctl00_RightContentPlaceholder_lbAregCity\">XXXXX</span></td></tr></table>";
org.jsoup.nodes.Document doc = Jsoup.parse(html);
Iterator<Element> productList = doc.select("table[class=InfoTable]").iterator();
while (productList.hasNext()) {
//Do some processing
Element descLi = productList.next().select( "td:eq(1)").first();
String rr = descLi.text();
Log.d("TESTTT",rr );
}
I have a webpage that has a table containing different values, i want to retrieve the values of a defined . Example
<td align="center" valign="middle" style="font-size:12pt;width:17%;">10:00 PM </td>
<td class="paddingTop paddingBottom" align="center" style="width:20%;">
<img id="MainContent_ChannelDisplay2_GrdChannelProgs_imgProgThumbnail_26" src="../ProgramsImages/Movies/Bad%20Boys%20II.jpg" align="middle" style="border-color:White;border-width:1px;border-style:solid;width:95px;" />
</td>
<td class="Vdotline paddingTop" style="width:2%;"> </td>
<td class="BottomGreyBorder InTxt2 paddingTop" align="left" valign="top" style="width:64%;">
<table width="100%">
<tr>
<td>
<b><a id="MainContent_ChannelDisplay2_GrdChannelProgs_progLink_26" class="ShowingNowTitle" href="ProgramDetails.aspx?ProgramID=2320">Bad Boys II</a></b><br />
<div id="2320" class="statVal">
<span class="ui-rater">
<span class="ui-rater-starsOff" style="width: 90px;">
<span class="ui-rater-starsOn" style="width: 63px"></span>
</span>
<span class="ui-rater-rating">3.5</span>
(<span class="ui-rater-rateCount">2</span>)
</span>
</div>
I want to retrieve 10:00 PM. All other has the same structure but with different values like 8:00 PM, 6:00PM ...
Any idea please of how making that on JSOUP.
Since the only uniqueness of your desired td is that is the first one you can use :
Elements tds = document.getElementsByTag("td"); //then access the one at 0 index
or try
Elements tds = doc.select("td"); //then access the one at 0 index
take a look at the docs for more options...
Use selector-syntax to find elements
Use DOM methods to navigate a document
Edit
If you want to locate td of some a then you could do something like this:
Elements a= document.getElementsByTag("a");
then iterate over the a and take a peek at its child (td) some other method, look here Element Object child method
or if I got you wrong
Elements td= document.getElementsByTag("td");
then iterate over the td and take a peek at its child (a)