title: How to inject snippets of html into an string containing valid html?
body: <p>I have the following html (sized down for literary content) that is passed into a java method.</p>
  
  <p>However, I want to take this passed in html string and add a &lt;pre> tag that contains some text passed in and add a section of &lt;script type="text/javascript"> to the head.</p>
  
  <pre><code>String buildHTML(String htmlString, String textToInject)
  {
    // Inject inject textToInject into pre tag and add javascript sections
    String newHTMLString = &lt;build new html sections&gt;
  }
  </code></pre>
  
  <p>-- htmlString --</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>-- newHTMLString</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script type="text/javascript"&gt;
          window.onload=function(){alert("hello?";}
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
       &lt;div id="1"&gt;
          &lt;pre&gt;
            &lt;!-- Inject textToInject here into a newly created pre tag--&gt;
          &lt;/pre&gt;
       &lt;/div&gt;
    &lt;body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What is the best tool to do this from within java other than a regex?</p>