dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [F8] XML HELP (adding childNode)

  1. #1
    Senior Member
    Join Date
    Apr 2006
    Posts
    431

    [F8] XML HELP (adding childNode)

    Hi. I'm running into walls here. I am making my first website with xml. Now, I'm not great with XML, but I found a lot of tutorials and I'm starting to get the hang of it. But still, I need help if I want to finish my site.
    So here is my problem

    I have an XML file and a Flash page. The menu in the Flash page is loaded from the XML file. I would like to add a title (text) for that page, from the same XML file. I added a dynamic text field, added one more childNode and tried to link one another, I can't get it to work. Please help. S.O.S. . Thank you so much.

    Here si the XML file (original, not spoilled by me)
    PHP Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <menu>
          

       <menu name='web design'         link='web design.swf' act = 'load'/>
       <menu name='applications'         link='web design.swf' act = 'load'/>
       <menu name='identity'         link='web design.swf' act = 'load'/>
       <menu name='illustration'         link='web design.swf' act = 'load'/>
       <menu name='photography'         link='web design.swf' act = 'load'/>


      
       
    </menu>

    And here is the ActionScript(2) from the page:
    PHP Code:
    //start Y position of the first button
    startPosY 188;
    //start X position of the first button
    startPosX 160;
    //button heigth
    buttonHsize 19
    stop
    ();
    //Create new XML
    var myXML:XML = new XML();
    myXML.ignoreWhite true;
    //Create new arrays to store the values from the XML
    var links:Array = new Array();
    var 
    names:Array = new Array();
    var 
    acts:Array = new Array();
    //XML onload function
    myXML.onLoad = function(status) {
    //if xml has loaded successfully, create the menu
        
    if (status) {
            var 
    linkname:Array = this.firstChild.childNodes;
            for (
    i=0i<linkname.lengthi++) {
                
    names.push(linkname[i].attributes.name);
                
    links.push(linkname[i].attributes.link);
                
    acts.push(linkname[i].attributes.act);
                
    _root.attachMovie("button","btn"+i,_root.getNextHighestDepth());
                
    _root["btn"+i]._x startPosX;
                
    _root["btn"+i]._y startPosY;
                
    startPosY startPosY+buttonHsize;
                
    _root["btn"+(i)].blackTxt.Txt.text = (names[i]);
                
    _root["btn"+(i)].onRollOver btnOver;
                
    _root["btn"+(i)].onRollOut btnOut;
                
    _root["btn"+(i)].onRelease btnRelease;
                
    //here is the entering animation effect for the menu
    import mx.transitions.Tween;
    import mx.transitions.*;
    import mx.transitions.easing.*;
    TransitionManager.start(_root["btn"+i], {type:Flydirection:Transition.INduration:1.5easing:Strong.easeOutstartPoint:2});
    TransitionManager.start(_root["btn"+i], {type:Fadedirection:Transition.INduration:1.5easing:Strong.easeOut});
    new 
    Tween(trick_btn,"_alpha",Strong.easeOut,trick_btn._alpha,100,6,true)
    TransitionManager.start(menu_mc, {type:Flydirection:Transition.INduration:1.5easing:Strong.easeOutstartPoint:2});
    new 
    Tween(menu_mc,"_alpha",Strong.easeOut,menu_mc._alpha,100,6,true)
    //




                
    play();
            }
        } else {
            
    //if an error has occurred on load xml, this message will be displayed
            
    msgTxt="Error Loading Menu"
        
    }
    };

    //Load XML
    myXML.load("portfolio_menu.xml");
    //on rollOver function
    function btnOver() {
        
    this.gotoAndPlay(2);
    }
    //on rollOut function
    function btnOut() {
        
    this.gotoAndPlay(11);
    }
    //on Release function
    function btnRelease() {
        new 
    Tween(trick_btn,"_y",Strong.easeOut,trick_btn._y,this._y,1,true)
        new 
    Tween(trick_btn,"_x",Strong.easeOut,trick_btn._x,this._x,1,true)
        
    trick_btn._height 16.2
        
    var currentBtn:String this._name;
        var 
    currentIndex:String currentBtn.substring(35);
    //if act equals "url" open link in new window
        
    if (acts[currentIndex] == "url") {
            
    getURL(links[currentIndex],"_blank");
    //if act equals "load", load the movie listed at variables.xml
        
    } else if (acts[currentIndex] == "load") {
            
    loadMovieNum(links[currentIndex], 2);
        }

    Thanks

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    i have snipped the actionscript back to bare minimum to
    show how the txt attribute is parsed to an array
    XML file -
    PHP Code:
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    <menu> 
       <menu name='web design' txt='sometext0' link='web design.swf' act='load'/> 
       <menu name='applications' txt='sometext1' link='web design.swf' act='load'/> 
       <menu name='identity' txt='sometext2' link='web design.swf' act='load'/> 
       <menu name='illustration' txt='sometext3' link='web design.swf' act='load'/> 
       <menu name='photography' txt='sometext4' link='web design.swf' act='load'/> 
    </menu>
    in Flash -
    PHP Code:
    var myXML:XML = new XML(); 
    myXML.ignoreWhite true
    myXML.load("portfolio_menu.xml");

    var 
    txts:Array = new Array(); // added array to hold txt attribute
    var links:Array = new Array(); 
    var 
    names:Array = new Array(); 
    var 
    acts:Array = new Array(); 

    myXML.onLoad = function(status) { 
        if (
    status) { 
            var 
    linkname:Array = this.firstChild.childNodes
            for (
    i=0i<linkname.lengthi++) { 
                
    names.push(linkname[i].attributes.name);
                
    txts.push(linkname[i].attributes.txt); // feed txt attr. to array
                
    links.push(linkname[i].attributes.link); 
                
    acts.push(linkname[i].attributes.act); 
    }
    }
    }; 
    this produces txts array as below
    PHP Code:
    _level0.txts = [object #2, class 'Array'] [
        
    0:"sometext0",
        
    1:"sometext1",
        
    2:"sometext2",
        
    3:"sometext3",
        
    4:"sometext4"
      

    hth

  3. #3
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    Hi,

    Just reading this.. and I think that (if i am not wrong) "cristi_b_1" wanted to read a title for the flash page (to feed a dynamic tetx field) from the same xml ("portfolio_menu.xml") file, thus to add somewhere an XML element with the title string..

    Anyway, if this is the case, maybe this example could be usefull too:

    Your "portfolio_menu.xml" file could be:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    
    <title>My Page</title>
    
    <menu>        
       <menu name='web design'         link='web design.swf' act = 'load'/> 
       <menu name='applications'         link='web design.swf' act = 'load'/> 
       <menu name='identity'         link='web design.swf' act = 'load'/> 
       <menu name='illustration'         link='web design.swf' act = 'load'/> 
       <menu name='photography'         link='web design.swf' act = 'load'/>     
    </menu>

    In your AS that parses the XML file:
    PHP Code:
    var myXML:XML = new XML()
    myXML.ignoreWhite true

    var links=[]
    var 
    names=[]
    var 
    acts=[]

    myXML.onLoad = function(status) {
        if (
    status) {

            var 
    Title=this.firstChild.firstChild //.nodeValue
            
    TitleField.text=Title  // Assume "TitleField" is your text field 
            
    trace(Title)
             
            
    /*  Alternative method to get the "title":
            for (i=0; i<this.childNodes.length; i++) {
                
                var Node = this.childNodes[i]
                if (Node.nodeName == "title") {
                    var Title = Node.firstChild.nodeValue                
                    TitleField.text = Title  // Assume "TitleField" is your text field 
                    trace(Title)
                    break
                }

            }
            */
            
            
    var linkname:Array = this.childNodes[1].childNodes
            
            
    for (i=0i<linkname.lengthi++) {
                var 
    Attrributes=linkname[i].attributes
                names
    .push(Attrributes.name)
                
    links.push(Attrributes.link)
                
    acts.push(Attrributes.act)
            }
            
    trace(names+"\n"+links+"\n"+acts)
            
        }
    }

    myXML.load("portfolio_menu.xml" 
    Other than that, a_modified_dog's suggestion is the way it should be

    Regards!

    Kostas
    K. Zotos online portfolio: http://www.in3d.eu

  4. #4
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    Hi,

    I'd like to resend a better approach.

    Flash act as xml parser but not as validator, so you can read the xml example in the previous post with no problem. However a "well formed" xml, normally not allows 2 root (top level) nodes among others.. (we have to conform with rules)

    An improved (wellformed) xml can be:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    
    <portfolio>
    
      <title>My Page</title>
    
      <menu>        
         <menu name='web design'         link='web design.swf' act = 'load'/> 
         <menu name='applications'         link='web design.swf' act = 'load'/> 
         <menu name='identity'         link='web design.swf' act = 'load'/> 
         <menu name='illustration'         link='web design.swf' act = 'load'/> 
         <menu name='photography'         link='web design.swf' act = 'load'/>     
      </menu> 
    
    </portfolio>
    To read this file, the AS should modified and can be eg. like this:
    PHP Code:
    var myXML:XML = new XML()
    myXML.ignoreWhite true

    var links=[]
    var 
    names=[]
    var 
    acts=[]

    myXML.onLoad = function(status) {
        if (
    status) {
            var 
    Root=this.firstChild
            
            
    var Title=Root.firstChild.firstChild.nodeValue
            TitleField
    .text=Title  // Assume "TitleField" is your text field 
            
    trace(Title)
             
            
    /* Alternative method to get the "title":        
            for (i=0; i<Root.childNodes.length; i++) {
                
                var Node = Root.childNodes[i]
                if (Node.nodeName == "title") {
                    var Title = Node.firstChild.nodeValue                
                    TitleField.text = Title  // Assume "TitleField" is your text field 
                    trace(Title)
                    break
                }            
            }
            */
            
            
    var linkname:Array = Root.childNodes[1].childNodes
            
            
    for (i=0i<linkname.lengthi++) {
                var 
    Attrributes=linkname[i].attributes
                names
    .push(Attrributes.name)
                
    links.push(Attrributes.link)
                
    acts.push(Attrributes.act)
            }
            
    trace(names+"\n"+links+"\n"+acts)
            
        }
    }

    myXML.load("portfolio_menu.xml"
    Kostas
    Last edited by Kostas Zotos; 05-12-2008 at 03:27 PM.
    K. Zotos online portfolio: http://www.in3d.eu

  5. #5
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    Hi,

    I'd like to resend a better approach.

    Flash act as xml parser but not as validator, so you can read the xml example in the previous post with no problem. However a "well formed" xml, normally not allows 2 root (top level) nodes among others.. (we have to conform with rules)

    An improved (wellformed) xml can be:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    
    <portfolio>
    
      <title>My Page</title>
    
      <menu>        
         <menu name='web design'         link='web design.swf' act = 'load'/> 
         <menu name='applications'         link='web design.swf' act = 'load'/> 
         <menu name='identity'         link='web design.swf' act = 'load'/> 
         <menu name='illustration'         link='web design.swf' act = 'load'/> 
         <menu name='photography'         link='web design.swf' act = 'load'/>     
      </menu> 
    
    </portfolio>
    To read this file, the AS should modified and can be eg. like this:
    PHP Code:
    var myXML:XML = new XML()
    myXML.ignoreWhite true

    var links=[]
    var 
    names=[]
    var 
    acts=[]

    myXML.onLoad = function(status) {
        if (
    status) {
            var 
    Root=this.firstChild
            
            
    var Title=Root.firstChild.firstChild.nodeValue
            TitleField
    .text=Title  // Assume "TitleField" is your text field 
            
    trace(Title)
             
            
    /* Alternative method to get the "title":        
            for (i=0; i<Root.childNodes.length; i++) {
                
                var Node = Root.childNodes[i]
                if (Node.nodeName == "title") {
                    var Title = Node.firstChild.nodeValue                
                    TitleField.text = Title  // Assume "TitleField" is your text field 
                    trace(Title)
                    break
                }            
            }
            */
            
            
    var linkname:Array = Root.childNodes[1].childNodes
            
            
    for (i=0i<linkname.lengthi++) {
                var 
    Attrributes=linkname[i].attributes
                names
    .push(Attrributes.name)
                
    links.push(Attrributes.link)
                
    acts.push(Attrributes.act)
            }
            
    trace(names+"\n"+links+"\n"+acts)
            
        }
    }

    myXML.load("portfolio_menu.xml"
    THIS IS DUPLICATED.. SORRY. PLEASE DELETE IT. THANKS!

    Kostas
    Last edited by Kostas Zotos; 05-12-2008 at 03:31 PM.
    K. Zotos online portfolio: http://www.in3d.eu

  6. #6
    Senior Member
    Join Date
    Apr 2006
    Posts
    431
    Thank you so much for answering. Sorry it took so long for me to thank you but I have been to busy to check my mail . Thank you again for taking the time. I will check the scripts out right now
    Yes, KOSTAS That's exactly what I wanted to do. Add a title to the page, and the title text field is different from the content text field. Thank you
    Last edited by cristi_b_1; 05-21-2008 at 08:47 PM.

  7. #7
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    Quote Originally Posted by cristi_b_1
    Sorry it took so long for me to thank you but I have been to busy to check my mail..
    Ohh.. no problem!

    Don't mention it!

    Regards!

    Kostas
    K. Zotos online portfolio: http://www.in3d.eu

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