Created
September 27, 2017 20:01
-
-
Save juanbrujo/31aa6421e16c4085ffb6697c2f168ce3 to your computer and use it in GitHub Desktop.
Parse XML from RSS feed in vanilla JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from: // https://stackoverflow.com/a/17604251/2148418 | |
var parseXml; | |
if (typeof window.DOMParser != "undefined") { | |
parseXml = function(xmlStr) { | |
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml"); | |
}; | |
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) { | |
parseXml = function(xmlStr) { | |
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM"); | |
xmlDoc.async = "false"; | |
xmlDoc.loadXML(xmlStr); | |
return xmlDoc; | |
}; | |
} else { | |
throw new Error("No XML parser found"); | |
} | |
// use: | |
var xml = parseXml(response); | |
var self = xml.querySelectorAll('channel item')[0]; // first post from feed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment