Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Headache converting hardwired array data into XML loaded data

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
SeventhZen
Asked:
SeventhZen
1 Solution
 
TagGerrCommented:
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
 
SeventhZenAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now