We have a need to parse and extract the content from html files. We are thinking of using jQuery to easily navigate the DOM and extract a small piece of information. We found JavaScript library written in Java from Mo开发者_如何学JAVAzilla. Using this libary, we tried to load a file called file.js that includes jquery script as well as a few lines of jquery script code as shown below.
var content = $('<html> <body><div id="div1"><span> Hello World!</span></div></body></html>').find('div span').html();
print("content = " + content);
print("hello");
We got errors related to undefined document, navigator etc. which are in jQuery library. Can anyone please help us as to how to run jQuery scripts with Java or C# to parse html files.
Using Rhino from Java is fine, but you have to be aware of the fact that Javascript itself does not define the DOM API.
It is instead the role of the navigator that embeds the javascript engine.
You need to initialize the DOM by yourself, using for example the script found here: http://ejohn.org/blog/bringing-the-browser-to-the-server/
which allows to run jquery, according to the author, and then to load your html code in the `virtual' page that is emulated in this environment.
精彩评论