Returned XML into a JavaScript array

K

KDawg44

Hi,

I am getting some XML from an AJAX call and want to load it into a 2D
associative array.

For example, if my XML looks like this:

<XMLData>
<Item1>
<ItemName>Name1</ItemName>
<ItemDesc>Desc1</Desc>
<ImagePath>image1.jpg</ImagePath>
<Blah>asdf1</blah>
</Item1>
<Item2>
<ItemName>Name2</ItemName>
<ItemDesc>Desc2</Desc>
<ImagePath>image2.jpg</ImagePath>
<Blah>asdf2</blah>
</Item2>
.....
<ItemN>
<ItemName>NameN</ItemName>
<ItemDesc>DescN</Desc>
<ImagePath>imageN.jpg</ImagePath>
<Blah>asdfN</blah>
</Item1>
</XMLData>


I want to load it into an Associative Array like this:

array(ItemN, array(ItemName, NameN))

What is the easiest way to do this? I need to easily be able to
access various pieces in no particular order and access different
pieces of that so I need to get all the XML into the array and be able
to easily call to a specific item like itemName = array(Item14,
(ItemName))

I figured getting the data into the array would be the best way.

Thanks.

Kevin
 
K

KDawg44

Hi,

I am getting some XML from an AJAX call and want to load it into a 2D
associative array.

For example, if my XML looks like this:

<XMLData>
    <Item1>
         <ItemName>Name1</ItemName>
         <ItemDesc>Desc1</Desc>
         <ImagePath>image1.jpg</ImagePath>
         <Blah>asdf1</blah>
     </Item1>
    <Item2>
         <ItemName>Name2</ItemName>
         <ItemDesc>Desc2</Desc>
         <ImagePath>image2.jpg</ImagePath>
         <Blah>asdf2</blah>
     </Item2>
....
    <ItemN>
         <ItemName>NameN</ItemName>
         <ItemDesc>DescN</Desc>
         <ImagePath>imageN.jpg</ImagePath>
         <Blah>asdfN</blah>
     </Item1>
</XMLData>

I want to load it into an Associative Array like this:

     array(ItemN, array(ItemName, NameN))

What is the easiest way to do this?  I need to easily be able to
access various pieces in no particular order and access different
pieces of that so I need to get all the XML into the array and be able
to easily call to a specific item like itemName = array(Item14,
(ItemName))

I figured getting the data into the array would be the best way.

Thanks.

Kevin

OKay, I am doing pretty well here with this:

var xmlDoc = xmlHttp.responseXML;
var root = xmlDoc.getElementsByTagName('XMLData').item(0);
var items = new Array(root.childNodes.length);
for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
var node = root.childNodes.item(iNode);
items[root.childNodes.item(iNode).nodeName] = new
Array(node.childNodes.length);
for (i = 0; i < node.childNodes.length; i++) {
document.getElementById("apDiv4").innerHTML += "<br>NodeName =
" + node.childNodes.item(i).nodeName;
document.getElementById("apDiv4").innerHTML += "---- NodeValue = " +
node.childNodes.item(i).nodeValue;
items[root.childNodes.item(iNode).nodeName]
[node.childNodes.item(i).nodeName] =
node.childNodes.item(i).nodeValue;
}
}

The innerHTML is for debugging and all the nodeNames are coming
through perfect, but every nodeValue is coming back null.

Thanks for any insight.

Kevin
 
K

KDawg44

I am getting some XML from an AJAX call and want to load it into a 2D
associative array.
For example, if my XML looks like this:
<XMLData>
    <Item1>
         <ItemName>Name1</ItemName>
         <ItemDesc>Desc1</Desc>
         <ImagePath>image1.jpg</ImagePath>
         <Blah>asdf1</blah>
     </Item1>
    <Item2>
         <ItemName>Name2</ItemName>
         <ItemDesc>Desc2</Desc>
         <ImagePath>image2.jpg</ImagePath>
         <Blah>asdf2</blah>
     </Item2>
....
    <ItemN>
         <ItemName>NameN</ItemName>
         <ItemDesc>DescN</Desc>
         <ImagePath>imageN.jpg</ImagePath>
         <Blah>asdfN</blah>
     </Item1>
</XMLData>
I want to load it into an Associative Array like this:
     array(ItemN, array(ItemName, NameN))
What is the easiest way to do this?  I need to easily be able to
access various pieces in no particular order and access different
pieces of that so I need to get all the XML into the array and be able
to easily call to a specific item like itemName = array(Item14,
(ItemName))
I figured getting the data into the array would be the best way.

Kevin

OKay, I am doing pretty well here with this:

   var xmlDoc = xmlHttp.responseXML;
   var root = xmlDoc.getElementsByTagName('XMLData').item(0);
   var items = new Array(root.childNodes.length);
   for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
      var node = root.childNodes.item(iNode);
      items[root.childNodes.item(iNode).nodeName] = new
Array(node.childNodes.length);
      for (i = 0; i < node.childNodes.length; i++) {
        document.getElementById("apDiv4").innerHTML += "<br>NodeName =
" + node.childNodes.item(i).nodeName;
        document.getElementById("apDiv4").innerHTML += "---- NodeValue = " +
node.childNodes.item(i).nodeValue;
        items[root.childNodes.item(iNode).nodeName]
[node.childNodes.item(i).nodeName] =
node.childNodes.item(i).nodeValue;
      }
   }

The innerHTML is for debugging and all the nodeNames are coming
through perfect, but every nodeValue is coming back null.

Thanks for any insight.

Kevin

I read that I need to use .text instead of .nodeValue. THis did not
work either as it returned undefined.

Thanks again.

Kevin
 
K

KDawg44

OKay, I am doing pretty well here with this:
   var xmlDoc = xmlHttp.responseXML;
   var root = xmlDoc.getElementsByTagName('XMLData').item(0);
   var items = new Array(root.childNodes.length);
   for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
      var node = root.childNodes.item(iNode);
      items[root.childNodes.item(iNode).nodeName] = new
Array(node.childNodes.length);
      for (i = 0; i < node.childNodes.length; i++) {
        document.getElementById("apDiv4").innerHTML += "<br>NodeName =
" + node.childNodes.item(i).nodeName;
        document.getElementById("apDiv4").innerHTML += "---- NodeValue = " +
node.childNodes.item(i).nodeValue;
        items[root.childNodes.item(iNode).nodeName]
[node.childNodes.item(i).nodeName] =
node.childNodes.item(i).nodeValue;
      }
   }
The innerHTML is for debugging and all the nodeNames are coming
through perfect, but every nodeValue is coming back null.
Thanks for any insight.

I read that I need to use .text instead of .nodeValue.  THis did not
work either as it returned undefined.

Thanks again.

Kevin

As I am new to XML, I guess I did not understand that the text in
there was considered a child. Once I added a loop to another child, I
seem to be able to get to the value...

So I think I am doing better....

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,360
Latest member
kathdev

Latest Threads

Top