Solved

Headache converting hardwired array data into XML loaded data

Posted on 2007-11-29
2
234 Views
Last Modified: 2012-06-27
Hi all,

I can't seem to see the solution for this one.
I am trying to convert a hardcoded array into an XML file and then load that in.

at present the hardcoded file is presented like this

var productData:Array = [{title="XML for Flash" , cost:"29.99" , description:"A good book"} ,
{title="XML for Flash again" , cost:"39.99" , description:"Another good book"}
etc
];

so rather than have this all hard coded I wanted to create the identical array, but using XML as a source.
I setup my XML with the following
<products>
<book>
<title>XML for Flash</title>
<cost>29.99</cost>
<description>A good book</description>
</book>

<book>
<title>XML for Flash again</title>
<cost>39.99</cost>
<description>Another good book</description>
</book>

</products>

My problem is how do I convert this into the format

attached is my effort which wasn't right.

// XML version

var xml:XML = new XML();

xml.ignoreWhite = true;

 

// parse the XML file

xml.onLoad = function (bSuccess:Boolean) : Void {

	if(bSuccess==true) {

		var productData:Array = xml.firstChild.childNodes;

		for (var i:Number = 0; i < productData.length; i++) {

         // for each product node:

         var productnode:XMLNode = productData[i];

         productData.push(

            {i:i+1,

             title:productnode.attributes.title,

             cost:productnode.attributes.cost,

             description:productnode.attributes.description,

	});

      } else {

	  trace('error reading XML');

      }

}

 

xml.load("products.xml");

Open in new window

0
Comment
Question by:SeventhZen
2 Comments
 
LVL 1

Accepted Solution

by:
TagGerr earned 500 total points
ID: 20408213
Hi,

There are a few errors here, but a quick few fixes and you should be on your way. First off, you have an extra comma at the end of your push statement and a missing curly brace that are generating a lot of errors in the compiler. So if you delete the comma after the description push section ("description:productnode.attributes.description,") and place a closing curly brace between "});" and "}else{", the code will almost compile.

What you will get now is an infinite loop because you are adding to the original array productData with your push statement, so it will never reach the length of the array. Just change this to some other variable name, i.e. productDataHolder, and you'll be just about set.

The final problem actually lies in the XML file. Your AS is looping through the XML looking for attributes, of which you currently have none. The easy fix is to just change the XML nodes inside the book node to just attributes. Here is what your XML file can look like:

<products>
      <book title="XML for Flash" cost="29.99" description="A good book" />
      <book title="XML for Flash again" cost="39.99" description="Another good book" />
</products>

With that and the changes listed above, you should be able to reference the variables through the new variable similar to this: productDataHolder[0].title.

The full AS code for your Flash file is in the code snippet. Let me know if that doesn't work or if you need more help.
// XML version

var xml:XML = new XML();

xml.ignoreWhite = true;

var productDataHolder:Array = new Array();

 

// parse the XML file

xml.onLoad = function (bSuccess:Boolean) : Void {

    if(bSuccess==true) {

        var productData:Array = xml.firstChild.childNodes;

        for (var i:Number = 0; i < productData.length; i++) {

        // for each product node:

            var productnode:XMLNode = productData[i];

            productDataHolder.push(

                {i:i+1,

                title:productnode.attributes.title,

                cost:productnode.attributes.cost,

                description:productnode.attributes.description

                });

            }

        } else {

            trace('error reading XML');

      }

}

 

xml.load("products.xml");

Open in new window

0
 
LVL 1

Author Comment

by:SeventhZen
ID: 20483526
Thanks for your help TagGerr, I can see how powerful XML is and I am taking gentle steps into arming my flash skills with this toolset.
Still all over the place with the parsing though ;-)
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

Suggested Solutions

Title # Comments Views Activity
Best way to Display MP4 Video 5 307
Error 2032 in all the browser 6 2,816
Crash with shockwave flash 8 237
Adobe Flash Player ActionScript error 6 60
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 been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

932 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

20 Experts available now in Live!

Get 1:1 Help Now