Solved

Dynamic Multi-Level Objects?

Posted on 2007-11-28
3
136 Views
Last Modified: 2012-06-27
I'm trying to create an array to hold the values from an XML file. The file mostly contains name/value pairs, but some of the nodes contain attributes. I'd like to store the results in an object with a format similar to this:

object.nodeName
object.nodeName.attribute1
object.nodeName.attribute2

I'm doing fine with generating the first level - the object.nodeName, but I keep getting "undefined" when I try to create the attribute. Here's the code I have so far:
function popObj(tempXML,tempObj) {

	for (var i=0; i<tempXML.childNodes.length; i++) {

		if (tempXML.childNodes[i].childNodes[0].nodeValue != undefined) {

			tempObj[tempXML.childNodes[i].nodeName] = tempXML.childNodes[i].childNodes[0].nodeValue;

			for (var j in tempXML.childNodes[i].attributes) {

				tempObj[tempXML.childNodes[i].nodeName][j] = tempXML.childNodes[i].attributes[j];				

			}	

		}

	}

}

Open in new window

0
Comment
Question by:katyjack
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 20475979
this will parse your xml into variables and arrays

https://filedb.experts-exchange.com/incoming/ee-stuff/6131-XMLParser.zip
0
 

Author Closing Comment

by:katyjack
ID: 31415462
Thanks - I've seen similar parsers before but I'm trying to teach this to myself so I was trying to write it on my own - I can use this to get the job done and look at the code to see how they did it.

Thanks -
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 20488117
to be honest I wouldn't have written it if I'd found one already as it got quite complicated with all the recursion etc. but I can understand you wanting to understand how it works.  I've just included attributes as well so if you want an updated copy let me know :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now