解析XMLHttpRequest()结果(使用XPath)

编程入门 行业动态 更新时间:2024-10-15 16:18:49
本文介绍了解析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", '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)

本文发布于:2023-11-28 16:14:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XMLHttpRequest   XPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!