File source = new File(this.Path);
Document report = null;
try {
report = Jsoup.parse(source, "UTF-8");
} catch (IOException e) {
System.out.println("Unable to open ["+source.getAbsolutePath()+"] for parsing!");
}
Elements dom = report.children();
dom.select("#summarytable tbody").append("<tr><td>onempla</td><td>twompla</td><td>threempla</td><td>fourmpla</td><td>fivempla</td><td>sixmpla</td></tr>");
if(!source.canWrite()) System.out.println("Can't write this file!") //Just check if the file is writable or not
BufferedWriter bw = new BufferedWriter(new FileWriter(source));
bw.write(dom.toString()); //toString will give all the elements as a big string
bw.close(); //Close to apply the changes
Question
I have a HTML file that contains a table with a variable amount of row. I'd like to be able to open that file, select the table, append a row (the amount of columns is fixed), and write this back to file file. I'm not quire sure how to proceed.
UPDATED: Here's a simplified but HTML-valid file:
<html>
<head>
<title>Light Template</title>
</head>
<body>
<div class="container">
<div class="starter-template">
<h1>CTS QA Test Report</h1>
<p id="suiteintrotext"></p>
<h1>Summary</h1>
<table class="table" id="summarytable">
<tr>
<th>#</th>
<th>Test Name</th>
<th>Author</th>
<th>Start/End time (dur.)</th>
<th>Avg. CPU%</th>
<th>Pass</th>
</tr>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
<td>four</td>
<td>five</td>
<td>six</td>
</tr>
</table>
</div>
</div>
</body>
</html>
The second row is there to get some data for the step-by-step troubleshooting in Eclipse
This is as far as I managed to go in Java:
public void appendRow() throws ParserConfigurationException{
File source = new File(this.Path);
Document report = null;
try {
report = Jsoup.parse(source, "UTF-8");
} catch (IOException e) {
System.out.println("Unable to open ["+source.getAbsolutePath()+"] for parsing!");
}
Elements table = report.select("#summarytable");
// Create row and td elements add append?
}
My problem at this point is how to add a row, which itself should have more elements (the cells/columns). The problem right after that point would be how to save this in the HTML file?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow