ntkachov
ntkachov

Reputation: 1232

jquery get request returning document instead of XML

I have a peice of code that i call inside of $("document).ready() in jquery that tries to open an xml file and parse it.

$.get('cal.xml', function(data){
    alert(data);
var xmlDoc = $.parseXML(data);
var $xml = $(xmlDoc);
});

the alert that pops up is "[object Document]" rather than the actual text of the xml which then throws a problem with $.parseXML(data) saying that "Uncaught Invalid XML: undefined" (implying that data is undefined).

here is the XML file

<?xml version="1.0"?>
<cal>
    <today>
        <event>
            <time>
                6:30pm EST
            </time>
            <title>
                nothing
            </title>
        </event>
    </today>
</cal>

Could someone help me simply read in this XML file and set it up for parsing?

Upvotes: 2

Views: 1453

Answers (3)

wukong
wukong

Reputation: 2597

code to convert string to XML object

function str2XML (str) {
       var xml;
       if (window.ActiveXObject) {
           xml = new ActiveXObject("Microsoft.XMLDOM");
           xml.async = "false";
           xml.loadXML(str);
       } else {
           var parser = new DOMParser();
           xml = parser.parseFromString(str, "text/xml");
       }
       return xml;
}

Upvotes: 0

istvan.halmen
istvan.halmen

Reputation: 3350

Try to set the dataType option to xml:

$.get('cal.xml', function(data){
    alert(data);
}, 'xml');

"data" should be at this point parsed xml.

Upvotes: 3

Rafay
Rafay

Reputation: 31033

here is the fiddle hope it'll help http://jsfiddle.net/ah2Y8/1/

OR

http://jsfiddle.net/ah2Y8/2/

Upvotes: 0

Related Questions