0

I am trying to get a item status every hourly basis and email(Html format) along with the past 4 hours data. I do not have db to store those past values, so I have planned to store them in the HTML file itself.

I have code to get html file contents as string but I need help to remove the bottom most row in the HTML table and append the new value to the first row because I have to send only past 5hours report. So while sending 7 pm report <tr><td>19-Mar 7.00 PM</td> <td>4</td> <td>31</td> <td>50181</td> <td>555</td></tr> should be added at <!--$NewRow to be inserted here$--> place in the code and text between <!--$ToBeRemoved_Start$-->and <!--$ToBeRemoved_End$-->should be removed.

I also know that it can be achieved by getting the index value of commented markers and play with the replace, append, concatenate.. in html but I need to know whether there is an easy way to achieve it using some libs like jsoup etc.. Please let me know your thoughts.

public String getHTML_String() {
    StringBuilder contentBuilder = new StringBuilder();
    try {
        BufferedReader in = new BufferedReader(new FileReader("emailOutput.html"));
        String str;
        while ((str = in.readLine()) != null) {
            contentBuilder.append(str);
        }
        in.close();
    } catch (IOException e) {
    }
    return contentBuilder.toString();

}

HtmlCodeBelow:

<!DOCTYPE html>
<html>

<head>
  <meta charset="ISO-8859-1">
  <title>Api Report</title>
  <style>
    table {
      font-family: arial, sans-serif;
      border-collapse: collapse;
      width: 100%;
    }
    
    td,
    th {
      border: 1px solid #dddddd;
      text-align: left;
      padding: 8px;
    }
    
    tr:nth-child(even) {
      background-color: #dddddd;
    }
  </style>
</head>

<body>
  <h2>Content Api</h2>
  <table>
    <tr>
      <th>Date Time</th>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
      <th>Col4</th>
    </tr>
    <!--$NewRow to be inserted here$-->
    <tr>
      <td>19-Mar 6.00 PM</td>
      <td>1.22%</td>
      <td>24.75%</td>
      <td>4.71 ms</td>
      <td>7.67</td>
    </tr>
    <tr>
      <td>19-Mar 5.00 PM</td>
      <td>1.22%</td>
      <td>24.75%</td>
      <td>4.71 ms</td>
      <td>8.67</td>
    </tr>
    <tr>
      <td>19-Mar 4.00 PM</td>
      <td>1.22%</td>
      <td>24.75%</td>
      <td>4.71 ms</td>
      <td>7.67</td>
    </tr>
    <tr>
      <td>19-Mar 3.00 PM</td>
      <td>1.22%</td>
      <td>24.75%</td>
      <td>4.71 ms</td>
      <td>7.67</td>
    </tr>
    <!--$ToBeRemoved_Start$-->
    <tr>
      <td>19-Mar 2.00 PM</td>
      <td>1.22%</td>
      <td>24.75%</td>
      <td>4.71 ms</td>
      <td>7.67</td>
    </tr>
    <!--$ToBeRemoved_End$-->
  </table>
</body>

</html>

1 Answer 1

2

This would be fairly simple to accomplish with JSoup because:

  1. You are the owner of the HTML contents so you know what they are.
  2. The contents will always have the same structure.

The following code could be used to accomplish it. Comments added for clarity.

// Parse the html string into a document
Document doc = Jsoup.parse(getHTML_String(), "UTF-8");

// Get the table you want to remove and the first row
Element rowToRemove = doc.getElementsByTag("tr").get(5);
Element firstRow = doc.getElementsByTag("tr").get(0);

// Create an element for the row you want to add    
Element rowToAdd = new Element("tr");
rowToAdd.html("<td>19-Mar 7.00 PM</td> <td>4</td> <td>31</td> <td>50181</td> <td>555</td>");

// Add that new row after the first row element
firstRow.after(rowToAdd);

// Remove the unwanted row from the doc 
rowToRemove.remove();

System.out.println(doc.html());
Sign up to request clarification or add additional context in comments.

1 Comment

nice and structured answer! +1 from me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.