[Project_owners] evaluate html document by XMLHttpRequest
eric.jung at yahoo.com
eric.jung at yahoo.com
Tue Aug 7 11:26:46 PDT 2007
This won't solve your problem necessarily, but you should use the responseXML property instead of parsing the responseText property with DOMParser().
http://developer.mozilla.org/en/docs/XMLHttpRequest#responseXML
----- Original Message ----
From: Foreningen Selvet - Jesper Staun Hansen <jesper at selvet.dk>
To: Project Owners <project_owners at mozdev.org>
Sent: Tuesday, August 7, 2007 12:33:50 PM
Subject: [Project_owners] evaluate html document by XMLHttpRequest
Hello.
I am having some trouble using element.evaluate(properties) with
documents fetched with XMLHttpRequest, so I am looking for a method to
do the same as this:
//*****
var roundinfo = {
runde : "56",
heroesid : null,
heroesnick : null
}
var path = '//table/tbody/tr/td';
var obj = document.evaluate(path,document,null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var k = 0; k < obj.snapshotLength; k++) {
obj2 = obj.snapshotItem(k).parentNode;
var nodes = obj2.childNodes;
dump("Found keys: "+nodes.length+"\n");
var menu = nodes[0].textContent.trim().split("#");
roundinfo.heroesnick = menu[0].trim();
roundinfo.heroesid = menu[1].trim();
dump("Nick is: "+roundinfo.heroesnick+"\n");
dump("ID is: "+roundinfo.heroesid+"\n\n");
}
//*****
And the document contains this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td colspan="3"><font face="arial" size="1" color="DarkTurquoise">Axe Decapitators #130303</font></td>
</tr>
</table>
</body>
</html>
And the dump would be
Nick is: Axe Decapitators
ID is: 130303
And I tried some things to make it with XMLHttpRequest:
//******* Attemp:
var req = new XMLHttpRequest();
req.overrideMimeType('text/html');
req.open('GET',
'http://www.URL_TO_FETCH_CONTAINING_THE_ABOVE_CODE.dk', true);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if(req.status != 404) {
var path = '//table/tbody/tr/td/font';
var domParser = new DOMParser();
var dom = domParser.parseFromString(req.responseText,
"text/xml").documentElement;
var obj =
dom.evaluate(path,dom,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var k = 0; k < obj.snapshotLength; k++) {
obj2 = obj.snapshotItem(k).parentNode;
var nodes = obj2.childNodes;
dump("Found keys: "+nodes.length+"\n");
var menu = nodes[0].textContent.trim().split("#");
roundinfo.heroesnick = menu[0].trim();
roundinfo.heroesid = menu[1].trim();
dump("Nick is: "+roundinfo.heroesnick+"\n");
dump("ID is: "+roundinfo.heroesid+"\n\n");
}
}
else
dump("Error loading page with status"+ reg.status +"\n");
}
};
req.send(null);
But this only results in this:
Error: syntax error
Source File: chrome://browser/content/browser.xul
Line: 1, Column: 62
Source Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">-------------------------------------------------------------^
Any tips on how to do this "right"?
My regards.
_______________________________________________
Project_owners mailing list
Project_owners at mozdev.org
http://mozdev.org/mailman/listinfo/project_owners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mozdev.org/pipermail/project_owners/attachments/20070807/f0c6975f/attachment-0001.html
More information about the Project_owners
mailing list