Solved

Headache converting hardwired array data into XML loaded data

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

713 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