Solved

Headache converting hardwired array data into XML loaded data

Posted on 2007-11-29
2
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Action Script commands 4 332
Can't play a movie website which used to be playable 4 704
Flash Slides/Presnetation 4 84
CD Rom not playing 13 89
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…
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…
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 what frame rate is, how to control it and what effect it has on the video.

738 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