A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 47

Thread: XML to AS3 to List component in flash

  1. #1
    Member
    Join Date
    May 2011
    Posts
    39

    Unhappy XML to AS3 to List component in flash

    I am completely new to as3 and it looks very complicated!

    What i need is to
    1. load an XML node and gathering report names, and listing these into a List component from flash cs4

    2. in turn when any report name is clicked or rolled over, (in a dynamic text field) the report description for that reportname will show!

    Here is my XML structure :
    <?xml version="1.0" encoding="utf-8"?>
    <reports lastupdate="9/14/2005" dir="">
    <report name="Product Installations and Removals" bgcolor="78A615" photo="admire_hh.jpg">
    <bgswf url="admire_hhbg.swf" />
    <desc> The report focuses provides visability of installations and removals down to account level. these reports include any product additions and removals on accounts based upon the install date and removal date of the product. </desc>
    </report>

    <report name="Product price variation report" bgcolor="000000" photo="admire_sg.jpg">
    <bgswf url="admire_sgbg.swf" />
    <desc> The report provides visability of discounts or promotions applied to accounts at product level</desc>
    </report>
    </reports>


    I can show you what i know so far, which can output some results into the flash output box, but i do not know how to arrange any of this for my requirements above:



    import fl.controls.List;
    import flash.text.TextField;

    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("report2.xml"));

    function LoadXML(e:Event):void
    {
    xmlData = new XML(e.target.data);

    //get all report descriptions
    var desc:String = xmlData.reportdetails.desc.text();

    //how to get attribute name for first array
    var rportname:String =xmlData.reportdetails.attributes()[0];
    //how to get attributes name for all arrays
    var rportname2:String =xmlData.reportdetails.@name;

    //output all report descriptions
    trace(desc);
    }




    This is probably such an easy task for most so please share your knowledge with me! will be very grateful

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Is your code wrong or is your xml wrong? You have reportdetails in your code, but that's not in your xml anywhere.

    You probably want to get each report or reportdetails node and process them separately, rather than try to get all the names, all the descriptions, all the urls, whatever.

    When you use e4x to get a selection from your document like xml.reportdetails, you get an XMLList. You can iterate over that XMLList using a for loop:
    Code:
    var reportNodes:XMLList = xml.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
      var report:XML = reportNodes[i];
      processReport(report);
    }
    
    function processReport(r:XML):void{
      var name:String = r.@name.toString();
      var desc:String = r.desc.toString();
      //etc.  Get what you need and build an entry in your List.
    }
    I'd suggest making a class to represent a Report so that you can keep everything together in one object.

  3. #3
    Member
    Join Date
    May 2011
    Posts
    39
    really sorry my mistake sent you the wrong XML,
    this shows how really new i am to coding and this stuff in general!!


    <?xml version="1.0" encoding="utf-8"?>
    <reports lastupdate="9/14/2005" dir="">
    <reportdetails name="Product Installations and Removals">
    <desc>The report focuses provides visability of installations and removals down to account level. these reports include any product additions and removals on accounts based upon the install date and removal date of the product.</desc>
    </reportdetails>

    <reportdetails name="Product price variation report">
    <desc>The report provides visability of discounts or promotions applied to accounts at product level</desc>
    </reportdetails>

    <reportdetails name="Product Snapshot">
    <desc>This report allows you to look at the latests promotion on the number of products by services type and also by customer type inclusive of status and the associated reportdetailsal rental.</desc>
    </reportdetails>

    <reportdetails name="Price plan snapshot">
    <desc>This report details the price plan and the count of services.</desc>
    </reportdetails>

    </reports>

  4. #4
    Member
    Join Date
    May 2011
    Posts
    39
    sorrry i am abit confused by your reply . i understand the XMLList which i assume loops gathering each name, description as you wrote in the function ProcessReport.

    So what would be a complete code.

    import fl.controls.List;
    import flash.text.TextField;

    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("report2.xml"));

    function LoadXML(e:Event):void
    {
    xmlData = new XML(e.target.data);

    var reportNodes:XMLList = xml.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
    var report:XML = reportNodes[i];
    processReport(report);
    }

    function processReport(r:XML):void{
    var name:String = r.@name.toString();
    var desc:String = r.desc.toString();
    //etc. Get what you need and build an entry in your List.
    }

    So how would i place the report name in a list component and place the description into a dynamicly loading text box, loading the reports corresponding description.

    Really really Sorry if i seem really desperate or i seem as if i am taking advantage or being lazy!! It is jsut because i am finding it very difficult and stressful taking on as3 and coding, with a deadline relying on the basics of as3.
    Truly appreciate your help '5tonsofFlax'

  5. #5
    Member
    Join Date
    May 2011
    Posts
    39
    this is tha actual XML



    <?xml version="1.0" encoding="utf-8"?>
    <reports lastupdate="9/14/2005" dir="">
    <reportdetails name="Product Installations and Removals">
    <desc>The report focuses provides visability of installations and removals down to account level. these reports include any product additions and removals on accounts based upon the install date and removal date of the product.</desc>
    </reportdetails>

    <reportdetails name="Product price variation report">
    <desc>The report provides visability of discounts or promotions applied to accounts at product level</desc>
    </reportdetails>

    <reportdetails name="Product Snapshot">
    <desc>This report allows you to look at the latests promotion on the number of products by services type and also by customer type inclusive of status and the associated reportdetailsal rental.</desc>
    </reportdetails>


    <reportdetails name="Price plan snapshot">
    <desc>This report details the price plan and the count of services.</desc>
    </reportdetails>

    <reportdetails name="Sales Report">
    <desc>This report shows the number of product sales and cessations over time, by customer type marketing caterogy and rationalized product descriptions</desc>
    </reportdetails>

    </reports>

  6. #6
    Member
    Join Date
    May 2011
    Posts
    39
    Hi i found an example http://www.kirupa.com/developer/mx/listbox.htm
    this is exactly what i need!! but gathering report names from XML and showing the description

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    So do you have it under control now?

  8. #8
    Member
    Join Date
    May 2011
    Posts
    39
    nope not really buddy! was hoping for your help actually!! would appreciate it, this is like a different language to me. literally!

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, I don't use the built in components because I don't use the Flash IDE, and I haven't really bothered with Flex specific stuff either. Are you using fl.controls.List, mx.controls.List, or spark.components.List?

    You're probably using fl.controls.List, since you hadn't mentioned Flex. Look at the livedocs, and see what you need to do to build one.

    Looks like the relevant method is addItem, which takes an Object with label and data properties. That Object should be your Report instance.

  10. #10
    Member
    Join Date
    May 2011
    Posts
    39
    yep im not using flex im using Flash cs4.

    yep i think to add to the list component you use add item . how do i add the XML reports into the list

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Step 0: create a List.

    Step 1: Create a Report class. Report should have properties for everything you need to know about a report. It should also have label and data properties. The label should be whatever text you want to appear in the List.

    Step 2: In ProcessReport, create a Report instance from the XML given.
    Step 3: use addItem to add that new Report to your List.

    Get that far. Then worry about the interactivity. Use a listener for ListEvent.ITEM_CLICK to detect when an item in your list is clicked. http://help.adobe.com/en_US/FlashPla...vent:itemClick

  12. #12
    Member
    Join Date
    May 2011
    Posts
    39
    Cheers mate thank you a lot, still baffled but really thanks for your help.

    So step one you mean create the list componant in flash.
    step 2: lines one and two gather all the reportdetails available in the XML right. and line three and four effectively puts them into an array called processReport?

    var reportNodes:XMLList = xml.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
    var report:XML = reportNodes[i];
    processReport(report);
    }

    function processReport(r:XML):void{
    var name:String = r.@name.toString();
    var desc:String = r.desc.toString();
    }


    so effectively all i would need to do from here is place the variable name and desc into the list?

    theList.text = name ;

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, processReport is a function, not an array. You should create an array and put them in that, but you can't call it processReport because you've already got something called that.

    The processReport function is incomplete. You'll need to do something like this:
    Code:
    function processReport(r:XML):void{
      var name:String = r.@name.toString();
      var desc:String = r.desc.toString();
      var report:Report = new Report(name, desc);
      someList.addItem(report);
      reports.push(report);
    }
    That assumes you have created a Report class, that it is suitable to pass to addItem, and that you have an array called reports in which to store your Report instances.

  14. #14
    Member
    Join Date
    May 2011
    Posts
    39
    oh rightttt this is really helpful i am learning about coding just from you really appreciate this. how did you get so good at this stuff then? it takes some complex understanding

  15. #15
    Member
    Join Date
    May 2011
    Posts
    39
    okay i am really worried and having a panic attack now my boss is going to kill me if i dont do this by tomorrow!

    I have attempted to mess with the bits of code you thankfully helped me with and have a final code of this: but i have not completed it properly it looks like! I will do anything for you if you help me understand and complete this code maybe i am just missing something.




    import fl.controls.List;
    import flash.text.TextField;


    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("report2.xml"));



    function LoadXML(e:Event):void
    {
    var reportNodes:XMLList = xmlData.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
    var report:XML = reportNodes[i];
    processReport(report);
    }


    function processReport(r:XML):void{
    var name:String = r.@name.toString();
    var desc:String = r.desc.toString();
    var report:Report = new Report(name, desc);
    rname.addItem(report);
    reports.push(report);
    }

    }

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please post code in [code] tags.

    Move processReport out of LoadXML. Don't nest functions without a reason.

    Is rname your List? Did you create a Report class? Are you getting errors?

  17. #17
    Member
    Join Date
    May 2011
    Posts
    39
    yep r name is my list component , sorry about that here it is without processReport in the LoadXML and i have tagged it.

    import fl.controls.List;
    import flash.text.TextField;


    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("report2.xml"));



    function LoadXML(e:Event):void
    {
    var reportNodes:XMLList = xmlData.reportdetails;
    for (var i:int = 0; i < reportNodes.length(); i++){
    var report:XML = reportNodes[i];
    processReport(report);
    }
    }


    function processReport(r:XML):void{
    var name:String = r.@name.toString();
    var desc:String = r.desc.toString();
    var report:Report = new Report(name, desc);
    rname.addItem(report);
    reports.push(report);
    }

  18. #18
    Member
    Join Date
    May 2011
    Posts
    39
    i get errors for these lines of code

    var report:Report = new Report(name, desc);
    rname.addItem(report);
    reports.push(report)
    mostly saying type was not found or undefined

  19. #19
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then you did not create a Report class. You also did not declare and instantiate your reports array.

  20. #20
    Member
    Join Date
    May 2011
    Posts
    39
    hmm so a class such as

    var report:String = name.labelField

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center