0

I'm trying to get the Style attribute from the HTML div tag using Jsoup. See the below is my div tag.

<div id="badge" class="er-badge" style="width: 576px; height: 369px; background: url(&quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;" tabindex="0" data-preview-purposes="checked">

from this div tag style I want to extract only the background: attribute. I can fetch the entire style properties using below code

String style = Element.attr("style");

But, I want to fetch only the background attribute value from this. Is it possible to extract that using Jsoup or Please tell any other easy way to extract the attribute. Thanks in advance.

samabcde
  • 6,988
  • 2
  • 25
  • 41
Kavi Chinna
  • 237
  • 2
  • 7
  • 18

1 Answers1

0

Happy to see this question instead of someone trying to use Regex to parse manually.

Just like we should not parse html ourself, parsing CSS manually is also undesired.

There is already available library - cssparser htmlunit-cssparser to do the job. We can get the CSS value of background as below:

import org.htmlunit.cssparser.dom.CSSStyleDeclarationImpl;
import org.htmlunit.cssparser.parser.CSSOMParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class ParseStyle {
    public static void main(String[] args) throws IOException {
        Element e = Jsoup.parseBodyFragment("""
                <div id="badge" class="er-badge" style="width: 576px; height: 369px; background: url(&quot;http://localhost:1020/er/ERImg/firmUploads/13854/LogoImg4.png&quot;) 0% 0% / cover no-repeat; z-index: 1; position: relative;" tabindex="0" data-preview-purposes="checked"> 
                 """);
        String style = e.select("div").attr("style");
        CSSOMParser parser = new CSSOMParser();
        CSSStyleDeclarationImpl decl = parser.parseStyleDeclaration(style);
        System.out.println(decl.getPropertyCSSValue("background"));
    }
}
samabcde
  • 6,988
  • 2
  • 25
  • 41
  • samabcde - Thanks for your suggestion, I have tried to implement the code but I couldn't see new SACParserCSS3(), I can able to import only new SACParser(). I couldn't use SACParserCSS3 in my application. I'm using maven. – Kavi Chinna Apr 14 '23 at 07:34
  • Are you using version 0.9.30? https://mvnrepository.com/artifact/net.sourceforge.cssparser/cssparser/0.9.30 – samabcde Apr 14 '23 at 08:02
  • version 0.9.23... I could see SACParserCSS3.class in that Jar file when I extract that jar. Then Why I couldn't use that? – Kavi Chinna Apr 14 '23 at 08:12
  • I don't see any problem using both version, I found a more actively maintained lib for css parsing and updated my answer, see if it works for you. – samabcde Apr 17 '23 at 15:30
  • The edited code uses https://mvnrepository.com/artifact/org.htmlunit/htmlunit-cssparser – RBRi Apr 19 '23 at 08:42