Solved

Dynamic Multi-Level Objects?

Posted on 2007-11-28
3
139 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
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 how to load their YouTube profile onto Flash Media Live Encoder.

770 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