<p>I'm downloading a web page (tag soup HTML) with XMLHttpRequest and I want to take the output and turn it into a DOM object that I can then run XPATH queries on. How do I convert from a string into DOM object?</p>
<p>It appears that the general solution is to create a hidden iframe and throw the contents of the string into that. There has been <a href="http://starkravingfinkle.org/blog/2007/03/how-to-parse-html-in-mozilla/" rel="nofollow noreferrer">talk</a> of updating <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMParser" rel="nofollow noreferrer">DOMParser</a> to support text/html but as of Firefox 3.0.1 you still get an <code>NS_ERROR_NOT_IMPLEMENTED</code> if you try.</p>
<p>Is there any option besides using the hidden iframe trick? And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up the code, etc)?</p>
<p><a href="http://mxr.mozilla.org/mozilla/source/browser/components/microsummaries/src/nsMicrosummaryService.js#2090" rel="nofollow noreferrer">This</a> is an example of why I'm looking for a solution other than the iframe hack, if I have to write <em>all</em> that code to have a robust solution, then I'd rather keep looking for something else.</p>

[{'answer_id': 3871, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 3, 'selected': False, 'text': "<p><strong>Try this:</strong></p>\n\n<pre><code>var request = new XMLHttpRequest();\n\nrequest.overrideMimeType( 'text/xml' );\nrequest.onreadystatechange = process;\nrequest.open ( 'GET', url );\nrequest.send( null );\n\nfunction process() { \n    if ( request.readyState == 4 &amp;&amp; request.status == 200 ) {\n        var xml = request.responseXML;\n    }\n}\n</code></pre>\n\n<p>Notice the <strong>overrideMimeType</strong> and <strong>responseXML</strong>. <br> The <code>readyState == 4</code> is 'completed'.</p>\n"}, {'answer_id': 3917, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 2, 'selected': False, 'text': "<p>Try creating a div</p>\n\n<pre><code>document.createElement( 'div' );\n</code></pre>\n\n<p>And then set the tag soup HTML to the innerHTML of the div. The browser should process that into XML, which then you can parse.</p>\n\n<blockquote>\n  <p>The innerHTML property takes a string\n  that specifies a valid combination of\n  text and elements. When the innerHTML\n  property is set, the given string\n  completely replaces the existing\n  content of the object. If the string\n  contains HTML tags, the string is\n  parsed and formatted as it is placed\n  into the document.</p>\n</blockquote>\n"}, {'answer_id': 3937, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 1, 'selected': False, 'text': "<p>So you want to download a webpage as an XML object using javascript, but you don't want to use a webpage? Since you have no control over what the user will do (closing tabs or windows or whatnot) you would need to do this in like a OSX Dashboard widget or some separate application. A Firefox extension would also work, unless you have to worry about the user closing the browser.</p>\n"}, {'answer_id': 3958, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 4, 'selected': True, 'text': '<p>Ajaxian actually had a <a href="http://ajaxian.com/archives/introducing-html-into-an-iframe-and-getting-it-back" rel="noreferrer">post on inserting / retrieving html from an iframe</a> today. You can probably use the js snippet they have posted there.</p>\n\n<p>As for handling closing of a browser / tab, you can attach to the onbeforeunload (<a href="http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx</a>) event and do whatever you need to do.</p>\n'}, {'answer_id': 9132, 'author': 'Nickolay', 'author_id': 1026, 'author_profile': 'https://Stackoverflow.com/users/1026', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>Is there any option besides using the hidden iframe trick?</p>\n</blockquote>\n\n<p>Unfortunately, no, not now. Otherwise the microsummary code you point to would use it instead.</p>\n\n<blockquote>\n  <p>And if not, what is the best way to do the iframe trick so that your code works outside the context of any currently open tabs (so that closing tabs won't screw up code, etc)?</p>\n</blockquote>\n\n<p>The code you quoted uses the recent browser window, so closing tabs won't affect parsing. Closing that browser window will abort your load, but you can deal with it (detect that the load is aborted and restart it in another window for example) and it doesn't happen very often.</p>\n\n<p>You need a DOM window for the iframe to work properly, so there's no clean solution at the moment (if you're keen on using the mozilla parser).</p>\n"}]
['https://Stackoverflow.com/questions/3868', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/163/']