Reputation: 1232
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
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
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