title: How to force the browser to stop parsing dynamically inserted code to HTML 4?
body: <p>I actually need to parse some really old HTML to a PDF file, I already have a jar that does this, but it only accepts legit XHTML code. So i have to parse my old HTML code so the jar will accept it. As I know very well how the HTML-code that I parse will look like my idea was to use the <a href="http://ejohn.org/blog/pure-javascript-html-parser/" rel="nofollow">HTML-Parser by John Resig</a> to parse certain tags (img, br, meta) with it to straight XML, which will have the needed effect (mostly closing tags) on them.</p>
  
  <p>My actual attempt looks like this:</p>
  
  <pre><code>function fixTags() {
      var tagsToParse = new Array( "br", "img", "input", "meta" );
  
      for(i = 0; i &lt; tagsToParse.length; i++) {
          var elements = document.getElementsByTagName(tagsToParse[i]);
          for(j = 0; j &lt; elements.length; j++) {
                  elements[j].outerHTML = HTMLtoXML(elements[j].outerHTML);
          }
      }
  }
  </code></pre>
  
  <p>The problem here is that the browser will interpret the new code for the element as HTML4, which leads him to changing back the stuff that i wanted to change. For example a <code>&lt;br&gt;</code> becomes a <code>&lt;br/&gt;</code> after going through the parser, but the browser will actually interpret this as HTML4 and the outerHTML property of the element will be <code>&lt;br&gt;</code> again.</p>
  
  <p>My first attempt to solve this was to force the document to be XHTML temporarily:</p>
  
  <pre><code>var root = document.getElementsByTagName("html")[0];
  root.setAttribute("xml", "http://www.w3.org/1999/xhtml");
  </code></pre>
  
  <p>but this doesn't seem to bother the browser at all in his behaviour.</p>
  
  <p>The "obvious" solution of building a string-tree out of the dom, replacing the strings there and traversing the tree to the string I want seems a bit too heavy and complex for this "little" problem, that's why I ask you.</p>
  
  <p>So if someone has an idea for an easier solution, I would be very happy, the application is IE-only so IE-exclusive solutions are accepted as well.</p>