Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xmlDoc.load() in Mozilla

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

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

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    ... 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:
    Code:
    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);
    }

  • #3
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Thanks for the help. It's given me something to start with.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.p...threadid=14463>, or how Vladdy did it at <http://codingforums.com/showthread.p...threadid=30449>.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    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!
    Last edited by Skyzyx; 01-15-2004 at 07:51 PM.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Have you looked closer at XMLHTTP and it's Mozillan counterpart? If your RSS file is Well Formed&trade; 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/secu...pts.html#privs>

    You have responseText and responseXML that you can use to actually read the file. See <http://msdn.microsoft.com/library/de...ttprequest.asp> and especially <http://msdn.microsoft.com/library/de...ttprequest.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.
    Last edited by liorean; 01-15-2004 at 08:06 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean,

    It all boils down to being required to use:
    Code:
    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.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i use the vladddy's script, but it freezes entire page in explorer while downloading contents from some file. could this be avoided?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •