Greasemonkey:使用 XPath 从远程 XML 文档获取元素
我试图用 Greasemonkey 脚本做的是:
读取一些远程 XML 文档;
将其转换为XML对象;
然后使用XPath获取其中的元素。
getElementsByTagName(TagName) 方法可以很好地处理我的 XML 对象,但 evaluate("XPath expression") 却不能。有什么建议吗?请参阅下面的代码:
GM_xmlhttpRequest({
method: "GET",
url: "http://www.someserver.com/atom.xml",
onload: function(response) {
if (!response.responseXML) {
var xmlDoc = new DOMParser().parseFromString(response.responseText, "application/xml");
}
// this section works fine and returns the data of the first <entry>..</entry>
var snapEntries = xmlDoc.getElementsByTagName("entry");
alert (snapEntries[0].data);
// this section doesn't work for unknown reason and returns nothing
var snapEntriesXpath = xmlDoc.evaluate("//entry", xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
alert (snapEntriesXpath.snapshotItem(0).data);
}
});
What Im trying to do with my Greasemonkey script is:
to read some distant XML document;
convert it into XML object;
and then use XPath to get the elements inside of it.
The getElementsByTagName(TagName) method works fine with my XML-object, but evaluate("XPath expression") doesn't. Any suggestions? See the code below:
GM_xmlhttpRequest({
method: "GET",
url: "http://www.someserver.com/atom.xml",
onload: function(response) {
if (!response.responseXML) {
var xmlDoc = new DOMParser().parseFromString(response.responseText, "application/xml");
}
// this section works fine and returns the data of the first <entry>..</entry>
var snapEntries = xmlDoc.getElementsByTagName("entry");
alert (snapEntries[0].data);
// this section doesn't work for unknown reason and returns nothing
var snapEntriesXpath = xmlDoc.evaluate("//entry", xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
alert (snapEntriesXpath.snapshotItem(0).data);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IIRC,
.data
属性不会出现在每种搜索中。您可能需要使用:
但可能还有其他几个问题在起作用。如果这不起作用,(1) 链接到确切的 XML 文件;如有必要,请使用 pastebin.com。 (2) 报告 Firefox 错误控制台 (CtrlShiftJ) 报告的内容。
IIRC, the
.data
attribute won't be present for every kind of search.You probably need to use:
But several other issues could be at play. If that doesn't do it, (1) link to the exact XML file; use pastebin.com if necessary. (2) Report what Firefox's error console (CtrlShiftJ) reports.