Skyzyx 01-15-2004, 07:51 AM I'm using the following script to read in valid RSS 2.0 feeds. My intended end-result will be to parse out specific bits of data, which on a normal day is a piece of cake.
The problem I'm having is this: I seem to be limited to files with the .xml file extension in Mozilla. IE/Win can read .xml, .rss, and .booger files with no problem (I kept the valid RSS document the same and just changed the exts.)
Whenever I do try to load a file with another extension in Mozilla, the browser starts utilizing 95%+ of my CPU, and just locks up entirely. Is this a bug in Mozilla that I should report, or is there another method for importing these other files in Mozilla?
Here's the code that works just peachy in Moz (.xml only), and IE (any ext):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XML-RSS Parser</title>
<style>
body { font-family:verdana; font-size:12px; }
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
var xmlFeed=false;
var gecko= (document.implementation && document.implementation.createDocument) ? true:false;
var ie= (window.ActiveXObject && document.all) ? true:false;
function importXML(FeedParserRSSFile)
{
// For Gecko Browsers
if (gecko)
{
xmlFeed = document.implementation.createDocument("", "", null);
xmlFeed.async=false;
}
// For IE/Win
else if (ie)
{
xmlFeed = new ActiveXObject("Microsoft.XMLDOM");
xmlFeed.async=false;
}
// If a browser doesn't support this, do nothing.
else return false;
// Load in the XML document...
xmlFeed.load(FeedParserRSSFile);
// If it's all good, return the object.
if (typeof xmlFeed != "undefined") return xmlFeed;
else return false;
}
var feed = importXML('skyzyx.xml');
document.write('<b>RSS Version:</b> '+feed.getElementsByTagName('rss')[0].getAttribute('version'));
//-->
</script>
</body>
</html>
... are the mime-types set correctly? Mozilla doesn't care whatsoever about extensions, as long as mime-types are set to text/xml. You could also try another method:
function loadXML(url, func) {
var request = new (window.ActiveXObject || XMLHttpRequest)('Msxml2.XMLHTTP');
request.open('GET', url);
request.onreadystatechange = function(evt) {
if (request.readyState == 4) func(window.event || evt, request.responseXML);
}
request.send(null);
}
Skyzyx 01-15-2004, 03:19 PM A couple of questions:
1) If I'm working on these locally, how would I set the mimetype?
2) I don't want this function to fire another function for processing. I simply want it to return the object that contains the external xml document. Does this script allow me to do that?
3) Would you mind explaining each line of this code so that I'm aware of what's going on in my application? It'd be much appreciated. :p
Thanks for the help. It's given me something to start with.
liorean 01-15-2004, 03:24 PM Mozilla is stricter than ie in it's handling, as always, and only handles 'text/xml' or files that does not contain the < character at all.
You have to override the mimetype to get it to work. See how I did it at <http://codingforums.com/showthread.php?s=&threadid=14463>, or how Vladdy did it at <http://codingforums.com/showthread.php?s=&threadid=30449>.
Skyzyx 01-15-2004, 07:44 PM I've implemented Vladdy's script, and it works like a charm (with a couple of minor tweaks to fit my purposes).
What I've done is that since IE has no problems with this (it's much looser), I've left the IE code alone. I've simply moved "xmlFeed.load(FeedParserRSSFile);" up inside the IE condition.
Since Moz was having an impossible time reading anything other than .xml files, I feed the URL through Vladdy's script, and have it return the properly-MimeTyped XML document back to me (using the responseXML property).
Here's the new code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XML-RSS Parser</title>
<style>
body { font-family:verdana; font-size:12px; }
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
var xmlFeed=false;
var gecko= (document.implementation && document.implementation.createDocument) ? true:false;
var ie= (window.ActiveXObject && document.all) ? true:false;
// FIX MIMETYPES/DOCUMENT HEADERS FOR GECKO
// By Vladdy from CodingForums.com
function fixXmlMimeType(filename)
{
oxmlhttp = null;
try {
oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e) {
try {
oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
return null;
}
}
if (!oxmlhttp) return null;
try {
oxmlhttp.open("GET", filename, false);
oxmlhttp.send(null);
}
catch(e) {
return null;
}
return oxmlhttp.responseXML;
}
// IMPORT XML DOCUMENT
// Code from the Public Domain
function importXML(FeedParserRSSFile)
{
// For Gecko Browsers
if (gecko)
{
xmlFeed = document.implementation.createDocument("", "", null);
xmlFeed.async=false;
xmlFeed = fixXmlMimeType(FeedParserRSSFile);
}
// For IE/Win
else if (ie)
{
xmlFeed = new ActiveXObject("Microsoft.XMLDOM");
xmlFeed.async=false;
xmlFeed.load(FeedParserRSSFile);
}
// If a browser doesn't support this, do nothing.
else return false;
// If it's all good, return the object.
if (typeof xmlFeed != "undefined") return xmlFeed;
else return false;
}
var feed = importXML('skyzyx.xml');
document.write('<b>RSS Version:</b> '+feed.getElementsByTagName('rss')[0].getAttribute('version'));
//-->
</script>
</body>
</html>
Everything works great now, except this: In IE6/Win, I can load an RSS file from my website. In Mozilla, I can't. I can load local files without a problem, though. I've got the feeling that the script isn't giving enough time to load the external RSS file before trying to perform functions with the data.
How would I get Moz to to hold on a sec so that my XML file can have time to load from online?
Any help is appreciated! :thumbsup:
liorean 01-15-2004, 08:03 PM Have you looked closer at XMLHTTP and it's Mozillan counterpart? If your RSS file is Well Formed™ XML, you should be able to use it for about the same things that you can use XMLDOM/DOMParser/[DOM2LS] for. Rememebr that only signed scripts may reach across realms (unless you are still using ie5.5w). <http://www.mozilla.org/projects/security/components/signed-scripts.html#privs>
You have responseText and responseXML that you can use to actually read the file. See <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmobjxmlhttprequest.asp> and especially <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmobjxmlhttprequest.asp> for documentation, Mozilla states that as the documentation for their version as well.
You can drop the importXML function and use only the one you copied from Vladdy.
Skyzyx 01-15-2004, 09:13 PM Thanks liorean,
It all boils down to being required to use:
if (gecko) netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
... if I want to be able to read-in a feed from somebody elses site. Argh! With that in place, I've got it all working. Thanks. :thumbsup:
looka 08-26-2004, 10:55 AM i use the vladddy's script, but it freezes entire page in explorer while downloading contents from some file. could this be avoided?
|
|