A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Match XML attribute name to text box instance name

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Posts
    25

    Match XML attribute name to text box instance name

    Hello! I have a list of items in XML that populate dynamic text boxes using the code below. I basically reference an attribute in the XML element to a specific instance name on a text box.

    This works great for a few items, but I need to do this for several hundred, perhaps a thousand, items in an XML. Rather than copy & paste my code for each item and call out each specific text box and attribute value, is it possible to modify this code to match an instance name to the XML attribute value? In other words, one set of code that says "if the text box instance name is "x," provide the price of "x" from XML, but if the name is "y," provide the price of "y."

    Thank you for your time!

    My xml code (3 item sample):
    PHP Code:
    <products>
        <
    info_source id="SQL">
            <
    item id="apples">$1.00</item>
            <
    item id="oranges">$2.00</item>
            <
    item id="bananas">$3.00</item>
        </
    info_source>
    </
    products
    My Actionscript:
    PHP Code:
    links_xml = new XML();
    links_xml.load("links.xml");
    links_xml.ignoreWhite true;
    links_xml.onLoad = function(true) {
        if (
    true) {
            for (var 
    0i<this.firstChild.childNodes.lengthi++) {
                if (
    this.firstChild.childNodes[i].attributes.id == "SQL") {
                    for (var 
    0s<=this.firstChild.childNodes[i].childNodes.length-1s++) {
                        if (
    this.firstChild.childNodes[i].childNodes[s].attributes.id == "apples") {
                            
    apples.text this.firstChild.childNodes[i].childNodes[s].childNodes;
                        } else if (
    this.firstChild.childNodes[i].childNodes[s].attributes.id == "oranges") {
                            
    oranges.text this.firstChild.childNodes[i].childNodes[s].childNodes;
                        } else if (
    this.firstChild.childNodes[i].childNodes[s].attributes.id == "bananas") {
                            
    bananas.text this.firstChild.childNodes[i].childNodes[s].childNodes;
                        } 
                    }
                }
            }
        }
    }; 

  2. #2
    Member
    Join Date
    Mar 2009
    Location
    Brooklyn, NY
    Posts
    77
    private var txtFieldNameArray("apples", "oranges", "bananas");

    Code:
    private var txtFieldNameArray("apples",  "oranges", "bananas");
    
    links_xml = new XML(); 
    links_xml.load("links.xml"); 
    links_xml.ignoreWhite = true; 
    links_xml.onLoad = function(true) { 
        if (true) { 
            for (var i = 0; i<this.firstChild.childNodes.length; i++) { 
                if (this.firstChild.childNodes[i].attributes.id == "SQL") { 
                
    				for(var j = 0; j < txtFieldNameArray.length; j++)
    				{
                                       for (var s = 0; s<=this.firstChild.childNodes[i].childNodes.length-1; s++) { 
                                            if (this.firstChild.childNodes[i].childNodes[s].attributes.id == txtFieldNameArray[j]) { 
                                            txtFieldNameArray[j].text = this.firstChild.childNodes[i].childNodes[s].childNodes;
    						}
    					}
    				}
                } 
            } 
        } 
    };
    Last edited by cessnajumpin; 06-26-2009 at 06:17 PM.
    Break it until it works.

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Posts
    25
    Thank you for replying, Cessna.

    I dropped your code into place but got an "Attribute used outside class" error. Any thoughts to get around that? Again, thank you for your time.

  4. #4
    Member
    Join Date
    Mar 2009
    Location
    Brooklyn, NY
    Posts
    77
    Is your constructor and everything set up correctly?

    http://board.flashkit.com/board/showthread.php?t=505014
    Break it until it works.

  5. #5
    Junior Member
    Join Date
    Nov 2007
    Posts
    25
    Thank you! Problem solved!

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