Papa Cheikh Cisse
Papa Cheikh Cisse

Reputation: 78

retrieve html inline style attribute value with jsoup

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!

Upvotes: 5

Views: 13746

Answers (3)

twkevinzhang
twkevinzhang

Reputation: 75

public static Map<String, String[]> getStyleMap(String styleStr) {
    Map<String, String[]> keymaps = new HashMap<>();
    // margin-top:-80px !important;color:#fcc;border-bottom:1px solid #ccc; background-color: #333; text-align:center
    String[] list = styleStr.split(":|;");
    for (int i = 0; i < list.length; i+=2) {
        keymaps.put(list[i].trim(),list[i+1].trim().split(" "));
    }
    return keymaps;
}

Result:

0 = {HashMap$Node@5713} "background-color" -> ["#333"]
1 = {HashMap$Node@5714} "color" -> ["#fcc"]
2 = {HashMap$Node@5716} "margin-top" -> ["-80px","!important"]
3 = {HashMap$Node@5717} "text-align" -> ["center"]

Upvotes: 1

Irshu
Irshu

Reputation: 8436

Another way to extract the style attributes would be:

public Map<String, String> getStyleMap(Element element) {
    Map<String, String> keymaps = new HashMap<>();
    if (!element.hasAttr("style")) {
        return keymaps;
    }
    String styleStr = element.attr("style"); // => margin-top:-80px !important;color:#fcc;border-bottom:1px solid #ccc; background-color: #333; text-align:center
    String[] keys = styleStr.split(":");
    String[] split;
    if (keys.length > 1) {
        for (int i = 0; i < keys.length; i++) {
            if (i % 2 != 0) {
                split = keys[i].split(";");
                if (split.length == 1) break;
                keymaps.put(split[1].trim(), keys[i + 1].split(";")[0].trim());
            } else {
                split = keys[i].split(";");
                if (i + 1 == keys.length) break;
                keymaps.put(keys[i].split(";")[split.length - 1].trim(), keys[i + 1].split(";")[0].trim());
            }
        }
    }
    return keymaps;
}

would fill the HashMap as:

0 = {HashMap$Node@5713} "background-color" -> "#333"
1 = {HashMap$Node@5714} "color" -> "#fcc"
2 = {HashMap$Node@5715} "font-family" -> "'Helvetica Neue', Helvetica, Arial, sans-serif"
3 = {HashMap$Node@5716} "margin-top" -> "-80px !important"
4 = {HashMap$Node@5717} "text-align" -> "center"

Upvotes: 0

user2183530
user2183530

Reputation:

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

Upvotes: 7

Related Questions