A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Actionscript and XML

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    6

    Actionscript and XML

    I'm trying to load my XML file into flash, when it gets to the point that it starts to load the XML.... it just stops...

    Here's the AS (Keyframe 1):
    PHP Code:
    myXML = new XML();
    myXML.ignoreWhite=true;
    myXML.load("castmembers.xml");
    myXML.onLoad = function(success) {
            
    thisXML myXML.firstChild.childNodes.length
            
    amount = []; 
            for (var 
    i=0i<thisXMLi++) {
                    
    amount[i] = myXML.firstChild.childNodes[i].attributes.name;
            }
            
    gotoAndStop(2);

    Keyframe 2
    PHP Code:
    for (var i=0i<thisXMLi++) {
            
    _root.createTextField("field_txt"+ii10i*2000);
            
    _root["field_txt"+i].autoSize true;
            
    _root["field_txt"+i].text amount[i];

    And Here's my XML file for 1 cast member, but there's 6 total:
    HTML Code:
    <?xml version='1.0' encoding='utf-8'?>
    <cast>
        <name>Mikey</name>
        <age>35 yrs</age>
        <height>5'6"</height>
        <weight>249.9 lbs</weight>
        <wloss>+1.4 lbs</wloss>
    </cast>
    And I want it to load here:


    I need to load stats for 6 different cast members and I'm a little confused on how to do that!

    Any help is appreciated it!! If i didnt write this clear enough or it's not understandable, let me know and I'll try to make it more clear!

    Thanks!!!!

    edit::
    forgot about the link to the movie:
    http://www.egzist.com/biggest_loser_v04.html - doesnt pull any XML though
    And a link to download my files if needed:
    http://s7.yousendit.com/d.aspx?id=22...K03JSIN9PJFKMU
    Last edited by 0efx; 01-25-2006 at 02:08 AM. Reason: added link

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    put a stop(); at the top of frame 1, and append _root or _level0 to the gotoAndStop.

    fyi... within the onLoad, scope shifts to the XML object. so you could say this.firstChild.childNodes.length rather than myXML.firstChild.childNodes.length; however, with the frame no longer scoped, the gotoAndStop could be getting boggled

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    6
    Thanks for the reply! For starters, I should have stated that I am pretty much a flash novice! Just recently I've begun to understand it a little.
    I've spent the past few hours trying a couple different ways of getting this effect, and

    I've finally found a solution, sorta... I'm able to pull info and place it properly on the movie. But I'm having a hard time figure out how to make the second cast members stats read my second node, and so on down the line to cast member six, along with the sixth node in the XML document.

    I made 5 dynamic text boxes, gave them all instance names.
    Then I put in this actionscript:
    PHP Code:
    doc = new XML();
    doc.ignoreWhite true;
    doc.load("castmembers4.xml");
    function 
    myOnLoad() {
        
    _root.name_txt.text doc.firstChild.childNodes[0].firstChild;
        
    _root.age_txt.text doc.firstChild.childNodes[1].firstChild;
        
    _root.height_txt.text doc.firstChild.childNodes[2].firstChild;
        
    _root.weight_txt.text doc.firstChild.childNodes[3].firstChild;
        
    _root.wloss_txt.text doc.firstChild.childNodes[4].firstChild;
    }

    doc.onLoad myOnLoad
    And that works for the first Button (Mikey). But the second one obviously it will read the same node, and I tried putting in secondnode but that didn't work. I've done some googling for "actionscript + reading secondnode" but I'm not sure if I was looking for the same thing.

    I've uploaded my files to yousendit.com at:
    http://s7.yousendit.com/d.aspx?id=2W...U2XPHZG5MFE4OM

    Pretty much my problem is I need to find out how to make my AS read the second nodes and lower. A link to a tutorial will do fine! Anything! Right now I'm forced to use 6 different XML files!!! GAH!

    Thanks for the help!

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    "properly formed" xml needs one (and only one) "root" node.

    since you're using cast as member, you'll need to reform it somewhat:
    Code:
    <?xml encoding='utf-8'?>
    <root>
      <cast>
        <name>Mikey</name>
        <age>35 yrs</age>
        <height>5'6"</height>
        <weight>249.9 lbs</weight>
        <wloss>+1.4 lbs</wloss>
      </cast>
      <cast>
        <name>Jim</name>
        <age>30 yrs</age>
        <height>5'9"</height>
        <weight>288 lbs</weight>
        <wloss>+11.4 lbs</wloss>
      </cast>
    </root>
    the above actually becomes the xml object.

    when you say var X=new XML(); you're just putting a label to that big ugly pointy string above. try a trace(X) to see.

    firstChild just gives back exactly what it says - the first child node.

    so if you had <bob><jim></jim><fred></fred></bob> and you queried bob.firstChild, you'd get back jim.

    childNodes gives you gack all of them. if you say bob.childNodes, you'd get back jim and fred. they're numbered for you, starting at 0. you can follow a childNode query with square brackets [] containing a number, and the player will return that number-eth element in the childNodes list. so jim is bob.childNodes[0] and fred is bob.childNodes[1].

    you can also use a loop to run through them all. this looks tricky and like it should make more sense - it's just convention, but very common and very handy:
    Code:
    for(var i=0;i<bob.childNodes.length;i++){
    var child=bob.childNodes[i];
    trace(child);}
    one bit that's tough to remember is that text counts as a child - so if you have <bob>sunday<jim></jim></bob> - bob.firstChild is the string "sunday" - as is bob.childNodes[0].

    using the modified xml sheet mentioned above, open a new fla and paste this into frame 1 actions, then test. hope it helps

    Code:
    var formatting=new TextFormat("Tahoma",11);
    var textblock=createEmptyMovieClip('',0);
    var xmlobj=new XML();
    
    xmlobj.ignoreWhite=true;
    xmlobj.onLoad=function(){
    	var children=this.firstChild.childNodes;
    	for(var i=0;i<children.length;i++){
    		var parent=textblock.createEmptyMovieClip(i,i);
    		parent._y=textblock._height+textfield._height;
    		for(var j=0;j<children[i].childNodes.length;j++){
    			var key=children[i].childNodes[j].nodeName;
    			var val=children[i].childNodes[j].firstChild;
    			var textfield=_root[key+(i+1)]=parent.createTextField(key,i+j,0,parent._height,0,0);
    			textfield.autoSize=textfield.html=true;
    			textfield.setNewTextFormat(formatting);
    			textfield.htmlText="<b>"+key+":</b> "+val;}}}
    			
    xmlobj.load("yourxmlsheet.xml");
    
    onMouseDown=function(){
    	trace(name1.text);
    	trace(wloss2.text);
    	trace(age2.text);}

  5. #5
    Junior Member
    Join Date
    Jan 2006
    Posts
    6
    Thanks a lot for the reply! Very intuitive.
    I just got home from work and I'm gonna start working on this here shortly.
    Thanks again for all the explanations. Definately helped me out!

    A quick question again, is there ways to make AS read specific nodes in the XML file? I was reading somewhere about some kind of xPath extension. Not sure though.

    Thanks again!

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