简体   繁体   English

jStyleParser无法解析我的html文件

[英]jStyleParser can't parse my html file

I'm trying to use the jStyleParser library ( part of the CSSBox project) to analyze the css of a generic web page and bind all the css information to the DOM of that page. 我正在尝试使用jStyleParser库(CSSBox项目的一部分)来分析通用网页的CSS,并将所有CSS信息绑定到该页面的DOM。 My goal is, given a web page, to have a DOM tree such that, for each node, I can get all the css informations realted to tha node. 我的目标是,在给定一个网页的情况下,拥有一个DOM树,以便对于每个节点,我都可以将所有的CSS信息都转换为该节点。

This is (part of) my code: 这是我的代码(的一部分):

System.out.println("Analizing "+Mylocalfile.getAbsolutePath()); 
StyleSheet ss = CSSFactory.parse(Mylocalfile.getAbsolutePath(), "UTF-8");
if (ss.isEmpty()) System.out.println("StyleSheet is void");    

The parse method seem to be unable to parse the file: the StyleSheet ss is infact void. 解析方法似乎无法解析文件:StyleSheet ss实际上是无效的。

Do you know why? 你知道为什么吗?

Your code is correct as long as MyLocalFile points to a CSS style sheet (*.css). 只要MyLocalFile指向CSS样式表(* .css),您的代码就是正确的。 In that case, you should obtain the parsed style sheet in ss but it is not bound to any DOM. 在这种情况下,您应该在ss中获得已解析的样式表,但它不与任何DOM绑定。 If you still obtain an empty style sheet, there might be a bug in the CSS code or even in the parser. 如果仍然获得空的样式表,则CSS代码甚至解析器中可能存在错误。 In that case, I recommend to report this to the jStyleParser forum at SourceForge . 在这种情况下,我建议将其报告给SourceForgejStyleParser论坛

If you want to assign the style definitions to a DOM ( MyLocalFile points to an HTML file), you should use the assignDOM method as described in the jStyleParser manual . 如果要将样式定义分配给DOM( MyLocalFile指向HTML文件),则应使用jStyleParser手册中所述的assignDOM方法。 In that case, you should parse the HTML document with a DOM parser and use the assignDOM method to automatically retrieve the referenced style sheets and compute the element styles. 在这种情况下,您应该使用DOM解析器解析HTML文档,并使用AssignDOM方法自动检索引用的样式表并计算元素样式。 You may find an example in src/test/DOMAssign.java in the jStyleParser source package. 您可以在jStyleParser源程序包的src / test / DOMAssign.java中找到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM