XML data into a single variable using JQuery

Shane174

New Member
Imagine you have a data.xml file:\[code\]<?xml version="1.0" encoding="UTF-8" ?><root> <item>value1</item> <item>value2</item> <item>value3</item></root>\[/code\]I'm trying to store all the data config into a single variable in order to use it in my .js code using this:\[code\]$(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);});\[/code\]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:\[code\]<root> <item>value1</item> <item>value2</item> <item>value3</item></root>\[/code\]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:\[code\]console.log(results.root.item);console.log(results.find("item"));\[/code\]But both give me errors.Maybe there is a better way to do this (meaning reading xml data into a single variable).Any suggestions?
 
Back
Top