How to find the element on <td> markup-JSOUP - android

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)

Related

webview save editable

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.

How to Parse HTML using JSoup

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 element from <td> in html with jsoup Android

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

Parse HTML table using JSOUP and display it to listview

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.

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