dongshao8125
2014-12-14 19:49
浏览 72

AS3从外部XML将变量加载到Textfield中

I have a php file which outputs an XML data and i also have a swf file coded with AS3 where i want to load the ouput of PHP in.

PHP Code:

 echo "<root>
           <Username>" . $xml1 . "</Username>
           <XMLNode2>" . $xml2 . "</XMLNode2>
            <XMLNode3>" . $xml3 . "</XMLNode3>
         </root>";

AS3 Code:

import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.xml.XMLDocument;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("playerData.php"));
var variables:URLVariables = new URLVariables();

function onLoaded(evt:Event):void
{
    var xDoc:XMLDocument = new XMLDocument();
    xDoc.ignoreWhite = true;
    loader.resultFormat = "e4x";
    var myXML:XML = loader.data as XML;
    xDoc.parseXML(myXML.toString());
    user_name.text = trace(xDoc.Username);
}

This is the error i get in AS3 code:

Access of possibly undefined property Username through a reference with static type flash.xml:XMLDocument.

Basically those TxtElements are dinamic text (like labels).

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwen7331 2014-12-14 20:37
    已采纳

    You can simply do :

    var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, onLoaded);
        loader.load(new URLRequest('playerData.php'));
    
    function onLoaded(evt:Event):void
    {   
        var myXML:XML = new XML(evt.target.data);
        trace(myXML.Username);                           // gives : the value of your php $xml1 var
    
        userField.text = (myXML.Username).toString();    // we don't need trace() to assing a value to a textfield
    
    }
    
    点赞 打赏 评论
  • dtdvbf37193 2014-12-14 20:21

    There's no need for XMLDocument here. You only need XML class.

    Take a look at the XML class example in its documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#includeExamplesSummary

    Here's a an example class that operates on your xml data:

    package 
    {
        import flash.display.Sprite;
        public class FlashTest extends Sprite 
        {
            public function FlashTest() 
            {
                var xml:XML = 
                <root>
                    <Username>Paul</Username>
                    <XMLNode2> </XMLNode2>
                    <XMLNode3></XMLNode3>
                </root>;
    
                trace("username is: " + xml.Username);
            }
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题