Question 1:
my app receives a node as an html string; this received node is to replace the existing node in an existing, known html file;
I traverse through the file's nodes until the one to be replaced is found; then I create a nodelist of the children of the received node;
let 'nd' be a reference to the existing node
and 'ch' be a reference to the aforementioned children nodelist;
then I replace the old node with the new one by:
<nd.setChildren(ch);>
is this the most efficient algorithm?
Question 2:
I first set my parser to the name of the existing file:
<parser = new Parser("nameOfExistingFile.html");>.
then after the old node has been replaced and the parsing finished
I use java.io methods < output = new BufferedWriter(new FileWriter("nameOfExistingFile.html", false));> to overwrite the existing file.
this works just fine if my app is stand-alone, but if my app is running in Tomcat the existing file cannot be written to after the
declaration of <parser = new Parser("nameOfExistingFile.html");>.
I've tried re-setting <parser> to null and to another file but without success.
does anybody have any ideas for a work-around?
thank you for your assistance
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The answer to question 1 depends on how you "traverse through the file's nodes", but as far as updating the contents, that is the fastest way, I think. If there is a known structure to the HTML file, you may be better off with an algorithm that avoids wandering down dead ends in the DOM, rather than just blind traversal.
As for question 2, it seems that the Tomcat container is caching the connection, or at least holding it open. You can get the URLConnection directly from the Parser with getConnection, but there isn't a direct way to close the file it's attached to, AFAIK. The ConnectionManager openConnection(string) first tries to open the file as a URL and if that fails, attempts it again with the "file://localhost" prefix, which succeeds. If you want to try some experiments, you can use:
URL url = new URL (file://localhost//mylocalfile.html");
URLConnection connection = url.openConnection ();
and see if you can rewrite the file after that. In other words, I don't think it's the parser's fault.
In the worst case, you can read the entire file into a string (and close the file), and if it begins with "<HTML>" just pass the string to the Parser like: new Parser(mystring).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Question 1:
my app receives a node as an html string; this received node is to replace the existing node in an existing, known html file;
I traverse through the file's nodes until the one to be replaced is found; then I create a nodelist of the children of the received node;
let 'nd' be a reference to the existing node
and 'ch' be a reference to the aforementioned children nodelist;
then I replace the old node with the new one by:
<nd.setChildren(ch);>
is this the most efficient algorithm?
Question 2:
I first set my parser to the name of the existing file:
<parser = new Parser("nameOfExistingFile.html");>.
then after the old node has been replaced and the parsing finished
I use java.io methods < output = new BufferedWriter(new FileWriter("nameOfExistingFile.html", false));> to overwrite the existing file.
this works just fine if my app is stand-alone, but if my app is running in Tomcat the existing file cannot be written to after the
declaration of <parser = new Parser("nameOfExistingFile.html");>.
I've tried re-setting <parser> to null and to another file but without success.
does anybody have any ideas for a work-around?
thank you for your assistance
The answer to question 1 depends on how you "traverse through the file's nodes", but as far as updating the contents, that is the fastest way, I think. If there is a known structure to the HTML file, you may be better off with an algorithm that avoids wandering down dead ends in the DOM, rather than just blind traversal.
As for question 2, it seems that the Tomcat container is caching the connection, or at least holding it open. You can get the URLConnection directly from the Parser with getConnection, but there isn't a direct way to close the file it's attached to, AFAIK. The ConnectionManager openConnection(string) first tries to open the file as a URL and if that fails, attempts it again with the "file://localhost" prefix, which succeeds. If you want to try some experiments, you can use:
URL url = new URL (file://localhost//mylocalfile.html");
URLConnection connection = url.openConnection ();
and see if you can rewrite the file after that. In other words, I don't think it's the parser's fault.
In the worst case, you can read the entire file into a string (and close the file), and if it begins with "<HTML>" just pass the string to the Parser like: new Parser(mystring).