Solved

Dynamic Multi-Level Objects?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
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…
The goal of the tutorial is to teach the user how to how to record live broadcast.
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.

744 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

13 Experts available now in Live!

Get 1:1 Help Now