Javascript to import xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gbrendemuehl
    New Member
    • Jan 2007
    • 7

    #1

    Javascript to import xml file

    I am trying to create a javascript bookmarklet to import an xml file that is located on my pc. While I have found numerous examples of code that is part of a web page, I cannot get it to work as a bookmarklet.

    The debug alerts are displaed until xmlDoc.load is called and then no error messages or anything else. This is running in Firefox 1.5.09 on windows 2000.

    Any suggestions?

    Code:
    javascript:function%20loadXML()
    {
    	var dbug = 1; 
    	var xmlfile = "c:\test.xml"; 
    	var xmlDoc; 
    	function import_xml(file) 
    	{
    		if (dbug) alert('ImportXML'); 
    		var xmlDoc; var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); 
    		var ie = (typeof window.ActiveXObject != 'undefined'); 
    		if (moz) 
    		{
    			xmlDoc = document.implementation.createDocument("", "", null); 
    			xmlDoc.onload = parse_xml;
    		} 
    		else if (ie) 
    		{
    			xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
    			xmlDoc.async = false;  
    			while(xmlDoc.readyState != 4) {}; 
    		}
    		if (dbug) alert('Load');
    		xmlDoc.load(file);
    		if (dbug) alert('Loaded');
    	}
    	function parse_xml() 
    	{
    		if (dbug) alert('parseXML');
    	}
    	if (dbug) alert('loadXML'); 
    	import_xml(xmlfile);
    }loadXML();void(null)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Two points: when testing for object detection, just use e.g.
    Code:
    if (window.ActiveXObject) // ie
    Secondly, in Firefox, etc. you cannot access local files (security hole in IE).

    See this page for an example of loading XML via AJAX.

    Comment

    Working...