A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Loading XML into a dynamic text field in MX??

  1. #1
    Registered User
    Join Date
    Sep 2000
    Posts
    186

    Loading XML into a dynamic text field in MX??

    Howzit.

    I have an xml file called "news.xml"; and in my flash movie I have a dynamic text field with the variable name "newsfield".

    This is the script I have :

    newsfield = new XML();
    newsfield.load("news.xml");

    I have never done this before... I'm not too sure if my script in the xml file is right either.

    A simple fla. showing me what to do would be excellent.

    Thanks for the help.

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112
    First suggestion:

    If you're using FMX - don't use variable names attached to text fields. Use an instance name, and assign the instances property 'text' that value you want to display.

    Second suggestion:

    Keep the text field and XML Object seperate. This will help you if you need to do anything other than import the XML in for display in that text field.

    .... and... I'm confused when you say the script in your XML might not be correct... if you mean your XML might not be well-formed, maybe you could you post an example of the XML script.
    Richard Lyman
    rich at lithinos.com
    www.lithinos.com

  3. #3
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Hey Vaykent,

    I happen to be having this problem as well. Here's what my XML code looks like, (greater/less than signs replaced by () for posting purposes)
    Code:
    (ALLNEWS)
    (NEWS date="Feb 23 2003"theText="This is a test of the news using XML."/)
    (NEWS date="Feb 15 2003"theText="This is a test of the news using XML.  This is old, its date is february 15th."/)
    (NEWS date="Jan 30 2003"theText="Now this is the oldest. Jan 30th."/)
    (/ALLNEWS)
    I'm pretty sure my parsing code works too, but pretty sure isnt close enough eh? Heh, heres what it looks like:
    Code:
    function loadNews()
    {
    datesArray = new Array();
    newsArray = new Array();
    // load the XML object
    _root.mcMessage.text = "Loading XML News Data...";
    objXML = new XML();
    objXML.onLoad = loadXML;
    objXML.load("news.xml");
    }
    
    // onLoad XML function
    function loadXML(success) {
    	if (success) {
    	_root.mcMessage.text = "";
    	_root.siteCount = this.firstChild.childNodes.length;
    	_root.pushArray();
    	} else { _root.mcMessage.text = "Error loading XML Data";
    	}
    }
    
    function pushArray() {
    	for (var count = 0; count < _root.siteCount; count++) {
    	var nodeObj = objXML.firstChild.childNodes[count];
    	datesArray[count] = nodeObj.attributes.date;
    	newsArray[count] = nodeObj.attributes.theText;
    	}
    }
    Hope that didnt take too much of your time
    Oh yes, I call the loadNews function first frame, and my text field has a keyframe on the first frame as well. Also, to text to make sure my text field was working, I substituted the call to my loadNews function for
    Code:
    mcMessage.text = "test test test"
    And that text appeared fine.

    Any Ideas?

    Thanks so much for your time

  4. #4
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112
    My first suggestion - don't use Arrays.

    My second suggestion - put everything related to your XML object on the first frame of the root timeline, then specifically target everything directly.

    Next.... use PHP tags to post XML, like the sticky suggests (you ~did~ read the sticky... right? ...).

    This code works:
    PHP Code:
    _root.createTextField("mcMessage"10,10,10,10,10);
    _root.mcMessage.autoSize="left";
    _root.mcMessage.text="PLOP";
    function 
    loadNews(){
        
    _root.mcMessage.text "Loading XML News Data...";
        
    _root.objXML = new XML();
        
    _root.objXML.onLoad _root.loadXML;
        
    _root.objXML.load("news.xml");
    }

    function 
    loadXML(success) {
        if (
    success) {
            
    _root.mcMessage.text "XML Loaded";
        } else {
            
    _root.mcMessage.text "Error loading XML Data";
        }
    }
    loadNews() 
    ... and this XML works:
    PHP Code:
    <ALLNEWS>
    <
    NEWS date="Feb 23 2003"theText="This is a test of the news using XML."/>
    <
    NEWS date="Feb 15 2003"theText="This is a test of the news using XML.  This is old, its date is february 15th."/>
    <
    NEWS date="Jan 30 2003"theText="Now this is the oldest. Jan 30th."/>
    </
    ALLNEWS
    ... and with an explanation of what you do with the arrays, I can show you how to not use them... but how to do it with the XML Object you've already created.
    Richard Lyman
    rich at lithinos.com
    www.lithinos.com

  5. #5
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Ahh, that fixed the problem. Thanks much One more thing, why do you suggest not using arrays? I've used them in other sites and theyre very helpful.

  6. #6
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112
    To me, taking an XML Object and placing it in an Array Object is like taking a String Object and placing it in a Character Object (if that existed).

    XML Objects have the same functionality that Arrays do, but there far moe expressive of heactual structure of the data.

    I would be interested in what you use the arrays for. Maybe I can suggest how to do it with the XML Object.
    Richard Lyman
    rich at lithinos.com
    www.lithinos.com

  7. #7
    Member
    Join Date
    Dec 2002
    Location
    Lawrence, Kansas
    Posts
    53
    Well as of now, I read in each childnode of the xml object into the array, one for date, and one for text. Using that I create buttons and a display where users can navigate archived news. The latest news appears at the top, in the slot [0].

  8. #8
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112
    So you're not using any Array Object methods like push or pull? You're just taking the data and creating buttons and text fields from it?
    Richard Lyman
    rich at lithinos.com
    www.lithinos.com

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