tags:

views:

74

answers:

2

hello., i would like to retrieve fullpath of a file and pass it to javascript.. the requirement is that i need to retrieve xml file using javascript.

+1  A: 

If you know the exact file up front, you can create a server-side program (i.e. service) to read the file, parse it and output it.

Then you'll just need to write some Javascript to make an AJAX call to this service (check out a Javascript library like Prototype or JQuery) to read the output of the service and thus the contents of the file.

Ciaran Archer
+1  A: 

If it is a file you can access relatively to your web page do something like:

var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
xmlDoc.async="false";
xmlDoc.load("abc.xml"); 

Assuming you have your web page next to the abc.xml...

This doesn't specify how to get full path to the XML - do youi still need it or loading it is enough?

For cross browser (from: http://developer.apple.com/internet/webcontent/xmlhttpreq.html)

var req;
loadXMLDoc("abc.xml");

function loadXMLDoc(url) {
    req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    try {
                req = new XMLHttpRequest();
            } catch(e) {
                req = false;
            }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    req = false;
                }
            }
        }
        if(req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send("");
        }
    }

    function processReqChange() {
        // only if req shows "loaded"
        if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {
                // ...processing statements go here...
                alert(req.responseXML);
            } else {
                alert("There was a problem retrieving the XML data:\n" + req.statusText);
            }
        }
    }
Dror
i will not be saving filename as .xml instead i will save it as abc.config which is also a xml file as like web.config file!!when i tried to load using xmlDoc.load("abc.xml"); it worked pretty well but when i loaded using xmlDoc.load("abc.config"); xmldoc is not loading..
SAK
see at: http://stackoverflow.com/questions/2046828/open-xml-file-with-different-extension-using-loadxmldoc Also note - the code I provided is NOT cross browser!!!
Dror
oh.. then i do need cross browser supportive !!!
SAK
thank you for the reply ..i have two things to do now..one is as how to load the file if the file name is saved with this extension(xxx.config)..another thing is xmlDoc.selectNodes(); is supported only for internet explorer and not for other browser..
SAK