PDA

View Full Version : How to pass vars from XML to a document class?



humbucker
August 2nd, 2009, 05:09 AM
Hi there,

I'm creating a flash project and would like to pilot the most variables (eg: colors, texts, sizes, ...) from a single XML file and then spread them in my document class.

Could you tell me how to mod this ?



public var yourName:String;
public var myXMLLoader:URLLoader = new URLLoader();


public function intro():void {
myXMLLoader.load(new URLRequest("xml/config.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
}

public function processXML(e:Event):void {
var myXML:XML = new XML(e.target.data);
yourName = myXML.@webName;
trace (yourName); // doesnt return anything
myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader = null;
}



That's how my xml is coded :


<?xml version="1.0" encoding="utf-8"?>

<webName>
<![CDATA[My name...]]>
</webName>




Right now I got no errors but I got nothing when tracing the "yourName" var.

I must be missing something,

Thank you sincerly,

wvxvw
August 2nd, 2009, 01:38 PM
@ is an attribute accessor, webName is the name of the element node, you access it with dot:

yourName = myXML.webName;
Your code in general should've generate error because when you try to access a non-existing attribute in the way you did, you should get a "no such property" error. It's strange you didn't, check if it was ever executed, maybe you have more problems.

xgraves
August 2nd, 2009, 01:45 PM
I try to stay away from CDATA myself, but I believe with E4X you don't need to do anything special to process it. I could be totally wrong, though. Anyway, your code is trying to get an attribute which might be the problem.

// your code:
yourName = myXML.@webName;
// should be:
yourName = myXML.webName;

humbucker
August 3rd, 2009, 05:27 PM
Huge thanks to both of you, I did this stupid fault.
Now evertyhing's tracing fine.

Have a nice day!