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);}
First, all this:
Can be replaced with just this:
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 theresponseText
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 theresponseText
property at the moment, your DOM Parser is choking on it.There was some moments in this task:
Final result is: