title: Bulletproofing SimpleXMLElement
body: <p>Everyone knows that we should always use DOM techniques instead of regexes to extract content from HTML, but I get the feeling that I can never trust the SimpleXML extension or similar ones.</p>
  
  <p>I'm coding a OpenID implementation right now, and I tried using SimpleXML to do the HTML discovery - but my very first test (with alixaxel.myopenid.com) yielded a lot of errors:</p>
  
  <pre><code>Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 27: parser error : Opening and ending tag mismatch: link line 11 and head in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: &lt;/head&gt; in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 64: parser error : Entity 'copy' not defined in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: &amp;copy; 2008 &lt;a href="http://janrain.com/"&gt;JanRain, Inc.&lt;/a&gt; in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 66: parser error : Entity 'trade' not defined in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: myOpenID&amp;trade; and the myOpenID&amp;trade; website are in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 66: parser error : Entity 'trade' not defined in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: myOpenID&amp;trade; and the myOpenID&amp;trade; website are in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 77: parser error : Opening and ending tag mismatch: link line 8 and html in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: &lt;/html&gt; in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 78: parser error : Premature end of data in tag head line 3 in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 78: parser error : Premature end of data in tag html line 2 in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: in E:\xampplite\htdocs\index.php on line 6
  
  Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
  </code></pre>
  
  <p>I recall there was a way to make SimpleXML always parse a file, independently if the document contains errors or not - I can't remember the specific implementation though, but I think it involved using DOMDocument. What is the best way to make sure SimpleXML always parses any given document?</p>
  
  <p>And please don't suggest using Tidy, I think the extension is slow and it's not available on many systems.</p>