Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamic Multi-Level Objects?

Posted on 2007-11-28
3
Medium Priority
?
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 1000 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 43

Expert Comment

by:Rob
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
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…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

636 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