Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is someone to help me retrieve with jsoup the value of the text-align style in this example ?

<th style="text-align:right">4389</th>

Here i want to get the value right

Thank you!

share|improve this question
    
Show us what you have implemented till now. Where are you stuck. –  Talha Ahmed Khan Jun 14 '13 at 12:22
    
i was trying to know if my expression is working with that Elements elt = doc.select("[style*='text-align']"); System.out.println(elt.size()); –  pacheikh Jun 14 '13 at 12:26
add comment

1 Answer

up vote 3 down vote accepted

You can retrieve the style attribute of the element and then split it by :.

Example:

final String html = "<th style=\"text-align:right\">4389</th>";

Document doc = Jsoup.parse(html, "", Parser.xmlParser()); // Using the default html parser may remove the style attribute
Element th = doc.select("th[style]").first();


String style = th.attr("style"); // You can put those two lines into one
String styleValue = style.split(":")[1]; // TODO: Insert a check if a value is set

// Output the results
System.out.println(th);
System.out.println(style);
System.out.println(styleValue);

Output:

<th style="text-align:right">4389</th>
text-align:right
right
share|improve this answer
    
this is the solution i had finally imagined but in a different way. Thank you @wartai for your help. –  pacheikh Jun 15 '13 at 14:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.