問題描述
您好,我想從 http://rss.news 之類的實時網址解析 xml/rss.yahoo.com/rss/entertainment 使用純 Java 腳本(不是 jquery).我用谷歌搜索了很多.沒有什么對我有用.任何人都可以幫助處理一段工作代碼.
Hi i want to parse xml/rss from a live url like http://rss.news.yahoo.com/rss/entertainment using pure Java Script(not jquery). I have googled a lot. Nothing worked for me. can any one help with a working piece of code.
推薦答案
(你不能用谷歌搜索很多.)一旦你有 解決了同源策略,如果資源使用 XML MIME 類型(在這種情況下,text/xml
),您可以執行以下操作:
(You cannot have googled a lot.) Once you have worked around the Same Origin Policy, and if the resource is served with an XML MIME type (which it is in this case, text/xml
), you can do the following:
var x = new XMLHttpRequest();
x.open("GET", "http://feed.example/", true);
x.onreadystatechange = function () {
if (x.readyState == 4 && x.status == 200)
{
var doc = x.responseXML;
// …
}
};
x.send(null);
(另請參閱 AJAX 和 XMLHttpRequest Level 2 其他事件處理程序屬性的規范 [工作草案].)
(See also AJAX, and the XMLHttpRequest Level 2 specification [Working Draft] for other event-handler properties.)
本質上:無需解析.如果您想訪問 XML 數據,請使用標準 DOM Level 2+ 核心 或 DOM Level 3 XPath 方法,例如
In essence: No parsing necessary. If you then want to access the XML data, use the standard DOM Level 2+ Core or DOM Level 3 XPath methods, e.g.
/* DOM Level 2 Core */
var title = doc.getElementsByTagName("channel")[0].getElementsByTagName("title")[0].firstChild.nodeValue;
/* DOM Level 3 Core */
var title = doc.getElementsByTagName("channel")[0].getElementsByTagName("title")[0].textContent;
/* DOM Level 3 XPath (not using namespaces) */
var title = doc.evaluate('//channel/title/text()', doc, null, 0, null).iterateNext();
/* DOM Level 3 XPath (using namespaces) */
var namespaceResolver = (function () {
var prefixMap = {
media: "http://search.yahoo.com/mrss/",
ynews: "http://news.yahoo.com/rss/"
};
return function (prefix) {
return prefixMap[prefix] || null;
};
}());
var url = doc.evaluate('//media:content/@url', doc, namespaceResolver, 0, null).iterateNext();
(為了方便,另請參見 JSX:xpath.js,不使用 jQuery 的命名空間感知 DOM 3 XPath 包裝器.)
(See also JSX:xpath.js for a convenient, namespace-aware DOM 3 XPath wrapper that does not use jQuery.)
但是,如果由于某些(錯誤)原因 MIME 類型不是 XML MIME 類型,或者如果 DOM 實現無法識別它,您可以使用最近瀏覽器中內置的解析器之一來解析 responseText
屬性值.有關適用于 IE/MSXML 的解決方案,請參閱 pradeek 的答案.以下應該適用于其他任何地方:
However, if for some (wrong) reason the MIME type is not an XML MIME type, or if it is not recognized by the DOM implementation as such, you can use one of the parsers built into recent browsers to parse the responseText
property value. See pradeek's answer for a solution that works in IE/MSXML. The following should work everywhere else:
var parser = new DOMParser();
var doc = parser.parseFromString(x.responseText, "text/xml");
如上所述進行.
在運行時使用功能測試來確定給定實現的正確代碼分支.最簡單的方法是:
Use feature tests at runtime to determine the correct code branch for a given implementation. The simplest way is:
if (typeof DOMParser != "undefined")
{
var parser = new DOMParser();
// …
}
else if (typeof ActiveXObject != "undefined")
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
// …
}
另請參閱 DOMParser
和 HTML5:DOM 解析和序列化(工作草案).
這篇關于使用 Java Script 從 URL 解析 XML/RSS的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!