CSS analysis with jSoup

I am trying to parse CSS DOM in Java and am already using jSoup for the same function for HTML. I was looking through the jSoup API (like Google, of course), but did not find CSS related classes. Is there a way to parse the CSS format in the DOM using jSoup or do I need another API?

+6
source share
1 answer

Jsoup cannot go through the CSS DOM, although you can access it by selecting a style / link on the tags.

Take a look at CSS Parser , it looks very promising.

InputSource source = new InputSource( new StringReader( "h1 { background: #ffcc44; } div { color: red; }")); CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null); CSSRuleList rules = sheet.getCssRules(); for (int i = 0; i < rules.getLength(); i++) { final CSSRule rule = rules.item(i); System.out.println(rule.getCssText()); } 

Output

 h1 { background: rgb(255, 204, 68) } div { color: red } 
+2
source

All Articles