I have XML data which looks like this:
<?xml version='1.0' encoding='ISO-8859-1'?>
<cocktail_list>
<cocktail>
<name> Blue Hawaiian </name>
<ingredient>
<quantity>1</quantity>
<shot>White Rum</shot>
</ingredient>
<ingredient>
<quantity>1</quantity>
<shot>Blue Curacao</shot>
</ingredient>
<ingredient>
<quantity>2</quantity>
<shot>Pineapple Juice</shot>
</ingredient>
<ingredient>
<quantity>1</quantity>
<shot>Coconut Cream</shot>
</ingredient>
</cocktail>
<cocktail>...
</cocktail_list>
Using Javascript I would like to create a nested/associative array (or using objects) to look like: arrayName[Blue Hawaiian[White Rum => 1, Blue Curaco => 2, Pineapple Juice => 1...], name[shot => quantity, ...]]
Now I know how to iterate through my XML, however I don't know how best to convert this into my array.
Any help would be appreciated.
use jquery.parseXML
Then you the obj is a json object you can manipulate with obj["cocktail_list"]["cock_tail"]["name"] I didn't consider the array here. For array like cock_tail, you will need to check whether it is already in the obj, and push it in if yes.
What you want to do is create an object graph, which you can readily do with a recursive traversal of your XML tree. Every JavaScript object is a map (aka "associative array," but I don't like that term since they aren't arrays). The properties of an object can be accessed via dotted notation with a literal (
obj.foo
), and via[]
notation with a string (obj["foo"]
):You can see how the
[]
notation combined with a string name can readily be used as you traverse your structure, to build up a graph. You'd access the result very similarly to your example, just slight differences. I'd probably opt for the map to be keyed by cocktail name, and then have aningredients
property listing the ingredients (either as an array of objects, or just using the ingredient name as a key). But you can opt not to have theingredients
property and to instead have the cocktail object directly contain the ingredients, for example:or of course
There are lots of different ways you can structure your resulting object graph, depending on how you want to access it and your personal style.
The component parts are:
Creating an object. That's easy:
Adding a property to an object using dotted notation a property literal:
Adding a property to an object using bracketed notation with the name of the property coming from a string expression rather than from a literal:
In your case, you might get the property name from an XML
nodeName
ornodeValue
.Putting an object inside another object. This is actually just assigning to a property, where the value you're assigning is an object reference:
Since you already know how to traverse your XML, that should get you going on building up an object graph from the results of that traversal. I'm not writing actual code to do it because, again, there are many decisions to make in terms of how you want to structure your object graph that are entirely up to how you want to do it.