Domanda

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>
È stato utile?

Soluzione

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.

Altri suggerimenti

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 );
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top