Jump to content
Sign in to follow this  
speedlearner

Storing XML data into a Javascript array

Recommended Posts

I used AJAX to retrieve concatenated data from a XML document. Now I need to store that data into a Javascript array. How do I do this?

 

My Javascript Code:

 

var SVG_Data
var Retrieved_Data
var Coordinate_Pair
var Element_List
var Counter
function Setup() {
SVG_Data = new XMLHttpRequest();
SVG_Data.onreadystatechange = function () {
if (SVG_Data.readyState = 4) {
Retrieved_Data = SVG_Data.responseText;
document.getElementById("Information").value = Retrieved_Data;
Coordinate_Pair = new Array();
Coordinate_Pair.push.apply(Retrieved_Data);
Element_List = new Array("Top_Ladder_Line","Middle_Ladder_Line", "Bottom_Ladder_Line");
for (Counter = 0; Counter < 3; Counter++) {
document.getElementById("Information").value = Coordinate_Pair[Counter];
document.getElementById(Element_List[Counter]).setAttribute("points", Coordinate_Pair[Counter]);}
}
}
SVG_Data.send();
}
The concatenated data I received from the eXist database:
"60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"

Share this post


Link to post
Share on other sites

First, don't use apply when you call push. If you want to break that data up you'll probably need to use a regular expression or another CSV parser, since you have commas inside the data.

Share this post


Link to post
Share on other sites

I began this project by storing my array data inside a XML document so I and the eXist database can find it easily. I retrieved this data by concatenating the data and then used an XMLHTTPRequest to send the data from the database to a Javascript variable. Now you're suggesting that I disassemble the data using a search pattern (regular expression) and a string search method before storing it inside a Javascript array? I find that very inefficient. I think Brendan Eich or one of his staff should create a more efficient means of transferring data from a variable to an array. Perhaps a new Javascript object is in order.

Share this post


Link to post
Share on other sites

Yeah, it's pretty inefficient, but that's what happens when you decide to take several pieces of data that include commas, and put them together with commas. If you used any other character that does not appear in the actual data then you can just split the string and be done with it. If you want to build it like a row of CSV data, though, then you're going to need to parse it using the rules of CSV. Brendan Eich did all of the work necessary, but he can't force programmers to make intelligent choices. If you want to put data together in a way that forces you to use an expensive parser to get it back out, then you are more than capable of doing that, the language is not going to stop you. You can also just use JSON instead of trying to build your own string representation of complex data, but it's up to you. Brendan isn't going to force you to do anything.

Share this post


Link to post
Share on other sites

Why did I place commas after the quotation marks? I was taught years ago that when you create a Javascript array, you have to place a comma after each piece of data. Had I known that I would have to separate the data once the computer retrieved it from the database, I would have used a different character to separate the array elements.

 

Now that I know this, I will try to create a Javascript object that will fill this void. Once I do, I will let you know.

Share this post


Link to post
Share on other sites

I was taught years ago that when you create a Javascript array,

But you're not creating an array, you have just a string of text there. If it's code, then yeah that's how you define an array. What you have isn't code, it's data. You created a string. If you want to convert that string to an array then there are several options, the most common of which is to just use a JSON string that you can natively convert to an array or object, e.g.:
var json_str = '["60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"]';var array = JSON.parse(json_str);
Note the additional brackets. It is the notation for defining an array like this:
var array = ["60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"];
With JSON it is just a string of text that can be evaluated to produce an object.Otherwise, use a different delimiter and then split it:
var str = "60,30 60,80 150,80 150,30#60,130 60,80 150,80 150,130#60,180 60,130 150,130 150,180";var array = str.split("#");
Those are 2 ways to do what you want to do. But, if you start with this:
"60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"
and you want to end up with this:
var array = ["60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"];
then you need to use a CSV parser because you chose a delimiter that is inside the data.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×