简体   繁体   中英

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. 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.

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.

Do you know why?

Your code is correct as long as MyLocalFile points to a CSS style sheet (*.css). In that case, you should obtain the parsed style sheet in ss but it is not bound to any DOM. If you still obtain an empty style sheet, there might be a bug in the CSS code or even in the parser. In that case, I recommend to report this to the jStyleParser forum at SourceForge .

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 . 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. You may find an example in src/test/DOMAssign.java in the jStyleParser source package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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