Solved

Headache converting hardwired array data into XML loaded data

Posted on 2007-11-29
2
233 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
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…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

743 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

10 Experts available now in Live!

Get 1:1 Help Now