有没有类似Jsoup的东西,但我需要解析CSS,而不是解析HTML,有没有类似的解析器可以创建CSS的Document
模型?这也许可以用来迭代到节点?
我需要做的是在CSS文件中查找并替换"url"。使用html,这在Jsoup中非常容易。然而,我不确定CSS是否可以做到这一点。
如果不存在这样的解析器,有什么选择?
也许这会对您有所帮助?
我刚刚推出了我自己的Java CSS流解析器,可在github上获得。这个解析器的与众不同之处包括:
- 它是一个流解析器,因此解析器处理程序将在解析每个项后立即接收所有新内容的通知
- 全面支持所有当前记录的At规则
- 自定义类
TokenSequence
和Token
简化了处理选择器等的过程 - 易于使用和理解
- 适用于验证或更高级的应用程序
- 可扩展:设计用于处理对CSS定义的更改
对于您的问题,只需使用基本的解析技术:
try
{
//First get the input stream. For example, from a file
InputStream is = new FileInputStream(new File("/path/to/my/CSS/main.css"));
//Then parse
CSSParser parser = new CSSParser(is, new DefaultCSSHandler());
parser.parse();
}
catch (Throwable t)
{
t.printStackTrace();
}
但是覆盖DefaultCSSHandler
中的一些方法来查找所需的标识符。
用于在Java中读写CSS2和CSS3文件的CSS库是来自https://github.com/phax/ph-css它基于JavaCC语法,同时支持CSS2和CSS3,还可以解析HTML样式的属性。
It supports the most common hacks "*", "_" and "$" which are not spec compliant
It supports CSS math - the calc() expression
It supports the @page rule
It supports the CSS3 media queries
It supports @viewport rules
It supports @keyframes rules
It supports @supports rules - quite new
It supports the @namespace rules
You can get source location information for the different elements (line + column number for start and end - both for the tag as well as for the complete construct)
这将帮助您
http://cssparser.sourceforge.net/
还有
http://sourceforge.net/projects/cssparser/