I'm looking for a CSS Parser in java. In particular my requirement is, for a given node/element in an HTML document, to be able to ask/get the css styles for that element from the Parser.
I know there is the W3C SAC interface and one or 2 implementations based on this - but turorials/examples appear non-existant.
Any help/points in right direction much appreciated.
Thanks
I've used CSSParser and I like it- it gives good feedback on errors as well.
Here's some sample code I've found and modified:
I needed a CSS parser for an own project, but I found "CSSParser" to be too tedious and inflexible to work with (but that could have just been me), so I ended up writing my own simple but functional CSS parser.
Feel free to use it if you want to :-)
OSBCP CSS Parser
I just rolled out my own CSS Stream Parser for Java, available on github. What sets this parser apart includes:
TokenSequence
andToken
simplify processes for handling selectors, etc.A CSS library for reading and writing CSS2 and CSS3 files in Java is ph-css from https://github.com/phax/ph-css It is based on a JavaCC grammar and supports both CSS2 as well as CSS3 and additionally lets you parse HTML style attributes.
Since May 21st, 2013 a JDK 1.5 version is also available, which makes it more interesting for Android development
jStyleParser provides exactly this functionality. It parses all the referenced style sheets and maps them to the DOM tree nodes.
If you struggle with CSSParser, because there seems to be no documentation at all, and you perhaps want to parse just a CSS String, like value from style parameter, here is my simple sample of use:
Big advantage of CSSParser is that it is currently in Maven. So if you look for something rather simple and straightforwardly usable CSSParser seems to be good option.
Notes: it does automatic conversion for colors from hex format to rgb() format, but provides no help with sizes with units, it sees them as list of values! Not so good.