有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的CSS解析器

有没有类似于Jsoup的语法,但是我需要解析CSS,而不是解析HTML,有没有类似的语法,可以创建CSS的Document模型?也许可以用来迭代节点

我需要做的是在CSS文件中找到并替换“url”。对于html,使用Jsoup很容易做到这一点。然而,我不确定这是否可能与CSS

如果不存在这样的解析器,有哪些选项


共 (4) 个答案

  1. # 1 楼答案

    用于在Java中读写CSS2和CSS3文件的CSS库是来自https://github.com/phax/ph-css的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)
    
  2. # 3 楼答案

    我刚刚推出了自己的CSS Java流解析器,可以在github上找到。使该解析器与众不同的是:

    • 它是一个流解析器,因此解析器处理程序将在解析每个项目后立即收到所有新内容的通知
    • 全面支持所有目前记录在案的At规则
    • 自定义类TokenSequenceToken简化了处理选择器等的过程
    • 易于使用和理解
    • 用于验证或更高级的应用程序
    • 可伸缩性:设计为能够处理对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中的一些方法以查找所需的标识符

  3. # 4 楼答案

    也许this可以帮你