signus
signus

Reputation: 1148

Copy and Edit HTML in Memory

I'd like to read in a HTML file I've specified, and instead of editing and saving the file, I'd like to use a StringBuilder to modify that data and save it in memory so that I may save it elsewhere or do various tasks to it.

Assuming I want to edit a similarly formatted HTML file:

<html>
     <body>
          <p>Data</p>
          <p>More data</p>
     </body>
</html>

I'd like to be able to input information after the last set of data "More data", meaning I'd be looking to input between the </p> and </body> headers.

I've heard of many HTML parsers and API's such as HTML Agility Pack. Would any of those be a feasible option for doing as I would like to do?

Any suggestions are useful!

Upvotes: 1

Views: 135

Answers (1)

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

jsoup looks like it will do the job. However, if you just want to insert stuff before the closing body tag it might be a lot easier to do a simple regex to find the closing tag. You might even be able to just do:

htmlString.indexOf("</body>")

And add whatever you need before that.

Upvotes: 1

Related Questions