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

查看:88
本文介绍了解析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).

我已经进入text string使用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);}


推荐答案

此任务有一些时刻:


  • 属性responseXML在不使用req.overrideMimeType的情况下等于null(在FireFox中)。在我开始使用req.overrideMimeType-属性后,responseXML已经不是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天全站免登陆