You could try out Dean Edward's solution: http://dean.edwards.name/weblog/2006/04/easy-xml/
Basically you create a new element with the tag name set to "xml". Then set the "src" property to the URL of an XML file.
Another way would be to load the XML document in an IFRAME from a URL in the same domain as the current Web page. As long as the XML document in the IFRAME is in the same domain name as the parent page, JavaScript can access the window and document objects inside the IFRAME, and therefore JavaScript can extract those values.
Perhaps something like this?
function loadXML(url, callback, context) {
var iframe = document.createElement("iframe");
iframe.onload = function() {
callback.call(context, iframe.contentDocument);
iframe.onload = null;
document.body.removeChild(iframe);
iframe = callback = context = null;
};
document.body.appendChild(iframe);
iframe.src = url;
}
You would use it like this:
<script type="text/javascript">
// Procedural or Functional code:
loadXML("/data.xml", function(doc) {
$(doc).find("foo").each(function() {
// do stuff
});
});
// Object oriented code:
var someObject = {
foo: function() {
loadXML("/data.xml", function(doc) {
this.extractData(doc);
}, this); // Notice the third argument: "this" sets the value of "this" in the callback
},
extractData: function(doc) {
$(doc).find("foo").each(function() {
// do stuff
});
}
};
</script>
Note: This is untested code