Parsing XMLHttpRequest() result (using XPath)

坚强是说给别人听的谎言 提交于 2019-12-21 22:45:06

问题


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).

I have gotten in text string variable the page's HTML using XMLHttpRequest() and responseText property.

After that I converted text string into xml object (DOMParser) and tried to use XPath.

In FireFox's console I saw error:

Node cannot be used in a document other than the one in which it was created

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);}

回答1:


There was some moments in this task:

  • Property responseXML has been equal null (in FireFox) without using req.overrideMimeType. After I start using req.overrideMimeType- property responseXML isn't null already, but I couldn't correctly use XPath still. Thus I used responseText property and DOMParser;
  • When we use document.evaluate method we should use it on HTMLDocument object that was created, not for main document object;
  • There are Cyrillic symbols on loaded page, so I should get result in the charset windows-1251 to use XPath properly

Final result is:

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
}



回答2:


First, all this:

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');

Can be replaced with just this:

var req = new XMLHttpRequest();

Because every browser has implemented a native XMLHttpRequest object for quite some time.

Second, when you get your response, look for it in the responseXML (responseXML) property of the XHR NOT the responseText property. This will return a Document object containing the nodes of the XML response which you can then parse using the Core DOM or an XML Parser if you like. But since you are using the responseText property at the moment, your DOM Parser is choking on it.



来源:https://stackoverflow.com/questions/40729785/parsing-xmlhttprequest-result-using-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!