A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Handle XML with Actionscript

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    7

    Handle XML with Actionscript

    I am quite new when it comes to actionscript. I want to have a next button that will control the text that is pulled in dynamically from an XML file. Here is an example of what I have so far:

    XML Code:
    <directory>
        <contact>
            <name>Tom</name>
            <address>123 River Rd</address>
            <phone>555-1234</phone>
        </contact>
        <contact>
            <name>Dave</name>
            <text>456 Lake Ln</text>
            <describe>555-4321</describe>
        </contact>
        <contact>
            <name>Steve</name>
            <text>789 Stream St</text>
            <describe>555-5678</describe>
        </contact>
    </directory>

    Actionscript Code:
    function loadXML(loaded) {

    if (loaded) {

    _root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
    _root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
    _root.describe = this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;
    describe_txt.text = _root.describe;
    name_txt.text = _root.inventor;
    comment_txt.text = _root.comments;
    } else {
      trace("file not loaded!");
    }
    }

    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);



    my_xml = new XML();
    my_xml.ignoreWhite = true;
    my_xml.onLoad = loadXML;
    my_xml.load("data/phonebook.xml");

    So when I open the swf it is showing the first contacts information (tom,123 River Rd, 555-1234). I would like to program next and back buttons to change the information displayed (show the next contact information or the previous contact information). For the life of me I can't figure out the actionscript to make this happen. I would appreciate any help I can get. Thanks in advance.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Place two buttons on the main timeline in the frame with this script. Give buttons instance names of 'btnNext' and 'btnPrev'.
    Code:
    stop();
    
    var my_xml = new XML();
    my_xml.ignoreWhite = true;
    my_xml.onLoad = loadXML;
    my_xml.load("phonebook.xml");
    var my_num = 0;
    var my_total = 0;
    
    btnNext.onPress = doNext;
    btnPrev.onPress = doPrev;
    
    function loadXML(loaded) {
    	if (loaded) {
    		my_xml = my_xml.firstChild;
    		my_num = my_total = my_xml.childNodes.length-1;
    		doNext();
    	} else {
    		trace("file not loaded!");
    	}
    }
    
    function doNext() {
    	(++my_num>my_total) ? my_num=0 : null;
    	updateFields();
    }
    function doPrev() {
    	(--my_num<0) ? my_num=my_total : null;
    	updateFields();
    }
    
    function updateFields() {
    	name_txt.text = my_xml.childNodes[my_num].childNodes[0].firstChild.nodeValue;
    	comment_txt.text = my_xml.childNodes[my_num].childNodes[1].firstChild.nodeValue;
    	describe_txt.text = my_xml.childNodes[my_num].childNodes[2].firstChild.nodeValue;
    }

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