JavaScript で HTML を Parse する
function parseHTML(text) { var createHTMLDocument = function() { var xsl = (new DOMParser()).parseFromString( ['<?xml version="1.0"?>', '<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">', '<output method="html"/>', '</stylesheet>'].join("\n"), "text/xml"); var xsltp = new XSLTProcessor(); xsltp.importStylesheet(xsl); var doc = xsltp.transformToDocument( document.implementation.createDocument("", "", null)); return doc; }; var doc = createHTMLDocument(); var range = doc.createRange(); doc.appendChild(doc.createElement("html")); range.selectNodeContents(doc.documentElement); doc.documentElement.appendChild( range.createContextualFragment(text)); return doc; }
こんな感じの function を定義しておくと
var doc = parseHTML(req.responseText);