我需要在 JavaScript 中从同一站点加载另一个页面的可变内容,然后从该内容中获取数据(解析 XML).
I need in JavaScript to load in variable contents of another page from the same site and then get data from that contents (parse XML).
我已经使用 XMLHttpRequest() 和 responseText 属性在文本字符串变量中获取了页面的 HTML.
I have gotten in text string variable the page's HTML using XMLHttpRequest() and responseText property.
之后,我将文本字符串转换为 xml 对象(DOMParser)并尝试使用 XPath.
After that I converted text string into xml object (DOMParser) and tried to use XPath.
在 FireFox 的控制台中我看到了错误:
In FireFox's console I saw error:
节点不能在它所在的文档之外的文档中使用已创建
Node cannot be used in a document other than the one in which it was created
如何将 XMLHttpRequest() 结果转换为文档对象以使用 XPath 对其进行处理?我应该如何使用 document.evaluate 和这个对象?有没有更简单的方法来完成我的任务?
How can I convert XMLHttpRequest() result into document object to process it using XPath? How I should use document.evaluate with this object? Is there the easier way to do my task?
textString=file_get_contents('my url');
var parser = new DOMParser();
xml = parser.parseFromString( textString, "text/xml" );
list = getI( "(//td[contains(text(), 'Total:')])[1]",xml);
// Error: Node cannot be used in a document other than the one in which it was created`enter code here`
// HOW USE getI function here? (document.evaluate)
function file_get_contents( url ) { // Reads entire file into a string
//
// + original by: Legaev Andrey
// % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
var req = null;
try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
try { req = new XMLHttpRequest(); } catch(e) {}
}
}
if (req == null) throw new Error('XMLHttpRequest not supported');
req.open("GET", url, false);
req.send();
return req.responseText;
}
function getI(xpath,elem){return document.evaluate(xpath,(!elem?document:elem),null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);}
在这个任务中有一些时刻:
There was some moments in this task:
最终结果是:
req = new XMLHttpRequest();
req.open("GET", 'http://my_url', false);
req.overrideMimeType('text/xml; charset=windows-1251'); // for Cyrillic
req.send(null);
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(req.responseText, "text/html");
var list = xmlDoc.evaluate("(//td[contains(text(), 'Total (Всего):')])[1]",xmlDoc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
if(list.snapshotLength>0){
// operations
}
这篇关于解析 XMLHttpRequest() 结果(使用 XPath)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)