CSS解析器解析字符串内容

萨玛斯·阿萨娜(Samarth Asthana)

我试图在Java项目中使用CSS解析器从文本输入的字符串中提取CSS规则/ DOM。

我遇到的所有示例都将css文件作为输入。有没有一种方法可以绕过文件读取并直接使用css文件的字符串内容。

因为我正在处理的类仅获取css文件的字符串内容,并且已完成所有读取工作。

现在我有了这个,其中“ cssfile”是要解析的css文件的文件路径。

InputStream stream = oParser.getClass().getResourceAsStream(cssfile);
InputSource source = new InputSource(new InputStreamReader(stream));
CSSOMParser parser = new CSSOMParser();
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
CSSRuleList ruleList = stylesheet.getCssRules();  
System.out.println("Number of rules: " + ruleList.getLength());

参考链接

萨玛斯·阿萨娜(Samarth Asthana)

我发现的一种解决方法是使用带有内容的StringReader创建一个Reader并为Input源设置characterStream。但是应该有更好的方法来做到这一点。

InputSource inputSource = new InputSource();
Reader characterStream = new StringReader(cssContent);
inputSource.setCharacterStream(characterStream);
CSSStyleSheet stylesheet = cssParserObj.parseStyleSheet(source, null,
            null);
CSSRuleList ruleList = stylesheet.getCssRules();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章