Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Data Integration / June 2005



Tip: Looking for answers? Try searching our database.

How to populate a DataProvider with XML and AS only

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vinco75 - 28 Jun 2005 18:11 GMT
Hi there,

after having spent 2 hours, I decided to get some help from you.

Here's what I'd like to do.

I have an XML file that I'd like to use to populate a DataGrid (in the future,
I'd like to do that also with a list).

I don't want to use the XML Connector and the Data Binding to do so in order
to keep my files as low as possible.

However I haven't been able to generate the array to populate the DataGrid
with a DataProvider. It works if I hard-code the array in AS but I'd like to
get the info from my XML File.

Could you please show me how to do this with ActionScript only?

That will save me big time!

Thanks a lot,
Vincent

My XML File:
<?xml version="1.0"?>
<pagesinfo>
<page id="0">
<pagename>homepage</pagename>
<pagecolor>2</pagecolor>
</page>
<page id="1">
<pagename>biography</pagename>
<pagecolor>0</pagecolor>
</page>
<page id="2">
<pagename>credits</pagename>
<pagecolor>1</pagecolor>
</page>
<page id="3">
<pagename>studio</pagename>
<pagecolor>2</pagecolor>
</page>
<page id="4">
<pagename>music</pagename>
<pagecolor>3</pagecolor>
</page>
<page id="5">
<pagename>contact</pagename>
<pagecolor>1</pagecolor>
</page>
</pagesinfo>

Snippet of my AS code:
// aPages is the array I use to populate the dataProvider

// (I want to get rid of this and use the XML File instead)

aPages = new Array({Page:"homepage", ColorStyle:"2", Preview:"off"},
{Page:"biography", ColorStyle:"0", Preview:"off"}, {Page:"credits",
ColorStyle:"1", Preview:"off"}, {Page:"services", ColorStyle:"3",
Preview:"off"}, {Page:"studio", ColorStyle:"1", Preview:"off"}, {Page:"music",
ColorStyle:"1", Preview:"off"}, {Page:"contact", ColorStyle:"1",
Preview:"off"});

// dgPagesListing is the datagrid I use on my scene.

dgPagesListing.dataProvider = aPages;
LuigiL - 28 Jun 2005 19:10 GMT
Just two hours? I've spent days figuring something out but I guess I'm a
patient guy...
Use the attached function. I've commented it so I guess you will understand
it. It's based on your XML-schema so if you change your schema...

function initXML(xmlpath_str:String):Void{
    var content_xml:XML=new XML(); // new XML-object to load the data
    var pages_array:Array=new Array(); // new Array that holds the data for the
DataGrid
    content_xml.ignoreWhite=true;
    content_xml.onLoad=function(success:Boolean){
        if(success){
            for(var i=0; i<this.firstChild.childNodes.length; i++){
                var shortcut=this.firstChild.childNodes[i]; // pointer to the childNodes
                // push the XML-values in the pages_array
                pages_array.push({Page:shortcut.childNodes[0].childNodes[0],
Colorstyle:shortcut.childNodes[1].childNodes[0]});
            }
            // assigning the content to the DataGrid
            dgPagesListing.dataProvider = pages_array; // you may need to correct the
path to dgPagesListing
            // this for-loop is just to check the content of the pages_array
            for(var i in pages_array){
                var page=pages_array[i].Page;
                var colorstyle=pages_array[i].Colorstyle;
                trace(page + ": " + colorstyle);
            }
        } else {
            trace("Error parsing the XML-file.");
        }
    };
    content_xml.load(xmlpath_str);
}

initXML("pagesinfo.xml");
vinco75 - 28 Jun 2005 19:36 GMT
Hi Luigi,

I know I'd probably could have found it with a few more hours but I'm still
relatively new with ActionScript and I'm not always sure about what I'm doing
:-/

Anyway, that's not an excuse!

I want to thank you so much for what you've done. This works like a charm and
I owe you a drink ;-)
I was pretty close to the solution but I'd have probably spent my day on it.
Needless to say, you saved my day!

I hope someday I'll help you as you did.

Thanks again,
Vincent
LuigiL - 29 Jun 2005 08:45 GMT
You're welcome. Good luck with further development.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.