解析 XMLHttpRequest() 结果(使用 XPath) [英] Parsing XMLHttpRequest() result (using XPath)

查看:23
本文介绍了解析 XMLHttpRequest() 结果(使用 XPath)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 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.overrideMimeType 的情况下,属性 responseXML 已等于 null(在 FireFox 中).在我开始使用 req.overrideMimeType-property responseXML is not null 之后,我仍然无法正确使用 XPath.因此我使用了 responseText 属性和 DOMParser;
  • 当我们使用 document.evaluate方法我们应该在创建的 HTMLDocument 对象上使用它,而不是用于主文档对象;
  • 加载时有西里尔字母页面,所以我应该在 charset windows-1251 中得到结果以正确使用 XPath

最终结果是:

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆