A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Can this be written more efficiently?

  1. #1
    Senior Member
    Join Date
    Oct 2000
    Location
    California
    Posts
    149

    Can this be written more efficiently?

    I am just starting to use XML and I have this function working.
    The code seems to be redundant but it works.
    Can this function be consolidated somehow? I'm also attaching my files that I am using.
    Thanks


    info = new XML();
    info.ignoreWhite = true;
    info.load("info.xml");
    info.onLoad = function(success) {
    for (var i = 0; i<info.firstChild.childNodes.length; i++) {
    display.text += "Name: " + info.firstChild.childNodes[i].attributes.name;
    display.text += "\nAddress: "+info.firstChild.childNodes[i].firstChild.firstChild.nodeValue;
    display.text += "\nTown: "+info.firstChild.childNodes[i].firstChild.nextSibling.firstChild.nodeValue;
    display.text += "\nState: "+info.firstChild.childNodes[i].firstChild.nextSibling.nextSibling.firstChild.nod eValue;
    display.text += "\nZip: "+info.firstChild.childNodes[i].firstChild.nextSibling.nextSibling.nextSibling.fi rstChild.nodeValue;
    display.text += "\nPhone: "+info.firstChild.childNodes[i].firstChild.nextSibling.nextSibling.nextSibling.ne xtSibling.firstChild.nodeValue;
    display.text += "\nWebsite: "+info.firstChild.childNodes[i].firstChild.nextSibling.nextSibling.nextSibling.ne xtSibling.nextSibling.firstChild.nodeValue;
    display.text += "\nDetails: "+info.firstChild.childNodes[i].firstChild.nextSibling.nextSibling.nextSibling.ne xtSibling.nextSibling.nextSibling.firstChild.nodeV alue;
    display.text += "\n\n";
    }
    };
    Attached Files Attached Files

  2. #2
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    I don't see anything major, the only things that might make it a bit neater are:

    1) Assign the XML to a variable eg:

    PHP Code:
    var myInfo info.firstChild.childNodes[i
    Then just refer to myInfo.attributes etc.

    2) You can make all the assignments in one big statement rather than a lot of little statements eg:

    PHP Code:
    myDoc += info.attibutes.name "\n" info.firstChild.firstChild.nodeValue ... ; 
    It probably won't make any difference to your code speed just make it look prettier.

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  3. #3
    Senior Member
    Join Date
    Oct 2000
    Location
    California
    Posts
    149
    Hey Luke, thanks for the reply but I don't quite understand what you wrote.
    I'm definitely trying to consolidate my code. Since I am new to XML I figured my actionscript was a little "overscripted".
    Is this not the case? Are XML nodes this tedious to access?

  4. #4
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    It depends, if you are good with copy and paste then xml nodes aren't to much of a problem to handle, however it doesn't leave you with the cleanest looking code.

    The best way I have found is to use recursive function calls which parse through the XML child nodes. I have a tutorial which gives an example:

    http://tupps.com/flash/latestnewsv2.html

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  5. #5
    Senior Member
    Join Date
    Oct 2000
    Location
    California
    Posts
    149
    awesome, thanks. I'll check out your tutorials.
    - P

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