# Reading Variables From SWFOBJECT and display xml content

**URL:** <https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834>\
**Category:** flash\
**Created:** [May 31, 2008, 1:32pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834 "2008-05-31T13:32:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mediaman12](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mediaman12](https://forum.kirupa.com/u/mediaman12)\
**Post date:** [May 31, 2008, 1:32pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/1 "2008-05-31T13:32:01Z")

</div>

I want to pass the state and city variable to the swf file from the swfobject and then display the content of the variable from the xml file.

SWFOBJECT  
\<script type=“text/javascript”\>  
YUE.onDOMReady(function() {var so = new SWFObject(“assets/promoKiosk.swf”,“amenities”,“628”,“600”,“8”,"#FFF" );  
so.addParam(“wmode”,“transparent”);  
so.addVariable(“state”, “CA”);  
so.addVariable(“city”, “los angeles”);  
so.write(“flashcontent”);  
});  
\</script\>

Promo XML  
\<?xml version=“1.0” encoding=“utf-8”?\>  
\<offers\>  
\<state id=“CA”\>  
\<city id=“los angeles”\>  
\<offer\>This is the offer\</offer\>  
\</city\>  
\<city id=“san fran”\>  
\<offer\>This is the offer\</offer\>  
\</city\>  
\</state\>  
\</offers\>

Actionscript 2.0:  
function findNode(parentNode:XMLNode,id:String){  
var nodeChildren:Array = parentNode.childNodes;  
var \_length = nodeChildren.length;  
for (var i=0; i \< \_length;i++){  
if(nodeChildren\*.attributes.id == id){  
return nodeChildren\*;  
}  
}  
}  
function displayNodeContents(cityNode:XMLNode){  
// Having issue displaying content  
}

var xml:XML = new XML();  
xml.ignoreWhite = true;  
xml.load(“promo.xml”);  
xml.onLoad = function(success){  
if(success){  
var stateNode = findNode(this.firstChild,state);  
var cityNode = findNode(stateNode,city);  
displayNodeContents(cityNode);  
}  
}

---

<div class="post-metadata">

**Author:** ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)\
**Post date:** [June 1, 2008, 8:12pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/2 "2008-06-01T20:12:33Z")

</div>

You solution is done and you will find it here…  
just love XML 🙂

[http://www.4shared.com/file/49757588/c8d38efc/swfobject\_xml.html](http://www.4shared.com/file/49757588/c8d38efc/swfobject_xml.html)

Add more State nodes with different offer data to check it out

---

<div class="post-metadata">

**Author:** ![mediaman12](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mediaman12](https://forum.kirupa.com/u/mediaman12)\
**Post date:** [June 2, 2008, 2:25pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/3 "2008-06-02T14:25:13Z")

</div>

Thanks. Your example rocks. How do I just put the data in the first frame in a simple dynamic text box?

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [June 2, 2008, 2:35pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/4 "2008-06-02T14:35:39Z")

</div>

BTW you can use flashvars to pass vars to flash when it loads. It seems much more simpler that using swfobject.

On the embed and object tag on the html ad a new property called “flashvars” and then put your vars as if it was an URL: ?city=la&state=california

For the reading and parsing the xml I recommend the sephiroth class called xml2Object  
[http://www.sephiroth.it/file\_detail.php?pageNum\_comments=10&id=129](http://www.sephiroth.it/file_detail.php?pageNum_comments=10&id=129)

It’s really easy to use, and then you have you data in an object automatically. It doesn’t matter how long or complex is your xml.

In you situation…

\<?xml version=“1.0” encoding=“utf-8”?\>  
\<offers\>  
\<state id=“CA”\>  
\<city id=“los angeles”\>  
\<offer\>This is the offer\</offer\>  
\</city\>  
\<city id=“san fran”\>  
\<offer\>This is the offer\</offer\>  
\</city\>  
\</state\>  
\</offers\>

It would be something like

data\_object.offers.city[0].offer.data //that would get This is the offer

or

data\_object.offers.city[0].attributes.id //that would get los angeles

No more xml flash gibberish (firstChild, etc) 😉

---

<div class="post-metadata">

**Author:** ![mediaman12](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mediaman12](https://forum.kirupa.com/u/mediaman12)\
**Post date:** [June 2, 2008, 3:22pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/5 "2008-06-02T15:22:01Z")

</div>

That may be something I will research. I was hoping to just get some help on the above example. I would like to get help on the displayOffer function to get the data into a simple dynamic text box area.

---

<div class="post-metadata">

**Author:** ![mediaman12](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mediaman12](https://forum.kirupa.com/u/mediaman12)\
**Post date:** [June 2, 2008, 6:05pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/6 "2008-06-02T18:05:21Z")

</div>

I got everything working great with a dynamic text box. Except I cannot get data to appear as html. I have render as html selected on the properties menu.

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [June 2, 2008, 9:44pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/7 "2008-06-02T21:44:13Z")

</div>

I think if you want html you need to put the text this way:

my\_textfield.html = true;  
my\_texfield.htmlText = “there’s no \<b\>spoon\</b\>”;

---

<div class="post-metadata">

**Author:** ![mediaman12](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mediaman12](https://forum.kirupa.com/u/mediaman12)\
**Post date:** [June 3, 2008, 12:36am UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/8 "2008-06-03T00:36:08Z")

</div>

I am using myText for the text field. But it will not show html.

//init TextArea component  
myText.htmlText = true;  
myText.wordWrap = true;  
myText.multiline = true;  
myText.label.condenseWhite=true;

//load css  
myTextStyle = new TextField.StyleSheet();  
myTextStyle.load(“promo.css”);  
myText.styleSheet = myTextStyle;

Ostate// from swfobject  
Ocity// from swfobject  
//----------------------  
var xml:XML=new XML();  
xml.ignoreWhite=true;  
xml.onLoad=xmlLoaded;  
xml.load(“promo.xml”);  
var dataArray:Array=new Array();

function xmlLoaded(success:Boolean)  
{  
if(success)  
{  
var totalNodes=xml.firstChild.childNodes.length;  
for(var i=0;i\<totalNodes;i++)  
{  
var currentNode:XMLNode=xml.firstChild.childNodes\*; // state node  
var StateObject:Object=new Object();  
var cityNames:Array=new Array();// temp array

```
		// process all cities of this state
		for(var j=0;j&lt;currentNode.childNodes.length;j++)
		{
			var cityNode:XMLNode=currentNode.childNodes[j];
			var city=new Object();
			city.name=cityNode.attributes.id; // city id
			city.offer=cityNode.firstChild.firstChild; // offer
			cityNames.push(city);
		}
		
		StateObject.State=currentNode.attributes.id;
		StateObject.Cities=cityNames;
		
		//-------------Store everything-------------------
		
		dataArray.push(StateObject);
	}
}

displayOffer();

```

}

function displayOffer()  
{  
// trace(dataArray[0].Cities[0].name);  
// access content like this…  
for(var i=0;i\<dataArray.length;i++)  
{  
if(dataArray\*.State==Ostate)  
{  
var citiArray:Array=dataArray\*.Cities;  
for(var j=0;j\<citiArray.length;j++)  
{  
if(citiArray[j].name==Ocity)  
{  
//var offer:String=citiArray[j].offer;  
var offer:String=citiArray[j].offer;  
[//Alert.Show](https://Alert.Show)(offer);  
[COLOR=“Red”]myText.htmlText = offer;[/COLOR]  
return;  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Pier25](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pier25](https://forum.kirupa.com/u/Pier25)\
**Post date:** [June 3, 2008, 9:06am UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/9 "2008-06-03T09:06:10Z")

</div>

Can you post a bit of the xml & html you’re sending?

---

<div class="post-metadata">

**Author:** ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)\
**Post date:** [June 3, 2008, 12:38pm UTC](https://forum.kirupa.com/t/reading-variables-from-swfobject-and-display-xml-content/261834/10 "2008-06-03T12:38:19Z")

</div>

hope this will do…

[http://www.4shared.com/file/49952939/58ee0f87/\_2\_\_swfobject\_xml.html](http://www.4shared.com/file/49952939/58ee0f87/_2__swfobject_xml.html)
