Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  92] [ 7]  / answers: 1 / hits: 58897  / 13 Years ago, wed, november 23, 2011, 12:00:00

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.


More From » xml-parsing

 Answers
53

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


(See also AJAX, and the XMLHttpRequest Level 2 specification [Working Draft] for other event-handler properties.)



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


(See also JSX:xpath.js for a convenient, namespace-aware DOM 3 XPath wrapper that does not use jQuery.)



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


Proceed as described above.



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


See also DOMParser and HTML5: DOM Parsing and Serialization (Working Draft).


[#88951] Tuesday, November 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocky

Total Points: 316
Total Questions: 108
Total Answers: 110

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;