Imagine you have a data.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<item>value1</item>
<item>value2</item>
<item>value3</item>
</root>
I'm trying to store all the data config into a single variable in order to use it in my .js code using this:
$(document).ready(function() {
'use strict';
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'xml',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
results = $.getValues("data.xml");
console.log(results);
});
If I refresh the page I get in results variable a Document object with fields like URL, baseURI, body ...
If I refresh again I get in results a #document object with the data from the data.xml:
<root>
<item>value1</item>
<item>value2</item>
<item>value3</item>
</root>
So the type of object returned changes each time someone goes to the url.
I have two questions about this:
How can I make the returned value always #document? (which contains the data from the .xml file)
How can I access to an element from #document?
I have tried using:
console.log(results.root.item);
console.log(results.find("item"));
But both give me errors.
Maybe there is a better way to do this (meaning reading xml data into a single variable).
Any suggestions?