Rectangle 27 0

java getting html inline style attribute value with jsoup?


Montag -> left:57px
Montag -> left:57px
Dienstag -> left:197px
Mittwoch -> left:337px
Donnerstag -> left:477px
Freitag -> left:617px
Pattern p = Pattern.compile("(?<=border-right-width:1px;)(.*)(?=;width:140px;)");
String elementInformation = "";
for (Element elem : names) {
    if (elem.text().contains("Montag")) {
        Matcher m = p.matcher(elem.attributes().toString());
        elementInformation = elem.text() + " -> ";
        while(m.find()){
            elementInformation += m.group();
        }
    }
}
System.out.println(elementInformation);
Pattern p = Pattern.compile("(?<=border-right-width:1px;)(.*)(?=;width:140px;)");
for (Element elem : names) {
    if (!elem.text().contains("Zeit")) {
        Matcher m = p.matcher(elem.attributes().toString());
        elementInformation += "\n";
        elementInformation += elem.text() + " -> ";
        while (m.find()) {
            elementInformation += m.group();

        }
    }
}

Going from this, the code below should produce your desired result:

This pattern will look for all characters that are between border-right-width:1px; and ;width:140px;

To get the attribute information, there is no simple way to do this using only Jsoup. You can get the attributes by calling the Element.attributes() method in Jsoup, but as far as I know you will have to use a regex matcher to select the information you want.

You can definitely use Jsoup the way you do it to find the correct element.

You can modify the for each loop and parse the same information for all elements, though it

You can set up a regex lookahead and lookbehind pattern that will check for occurences that matches your pattern.

and you'll get:

Note