ActionScript 3 parsing XML data - actionscript-3

My Flash Code
var myLoader:URLLoader = new URLLoader;
var xmlData = new XML();
myLoader.addEventListener(Event.COMPLETE, LoadXML);
myLoader.load(new URLRequest("mydata.xml"));
function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
trace(xmlData);
}
My XML Data (mydata.xml)
<xml>
<items>
<item Name="Test" ID="1" />
<item Name="Home" ID="2" />
<item Name="Car" ID="3" />
<item Name="Balloon" ID="4" />
<item Name="Harry" ID="5" />
<item Name="Lion" ID="6" />
</items>
</xml>
How can I get each item in the xml file to be able to then use it in my flash file.
I have tried several things but none seem to work. I am using ActionScript 3.0.

Using E4X you can loop over each item in your XML
for each (var item:XML in xmlData.items.item) {
trace(item.#Name, item.#ID);
}

I changed it to this xmlData..item and it worked. Does anyone know why?
for each (var item:XML in xmlData..item) {
trace(item.#Name, item.#ID);
}

Related

JSON: How to log a JSON object value from a vxml?

My aim is to log a JSON object value fethed from a jsp file from the vxml. Is there any way to do that. I see that there is a function called JSON.stringify but thats giving me nothing as a log.
Below is my code:
<?xml version="1.0"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
<var name="userId" expr="1" />
<!--form id="get_location"-->
<data name="userData" method="get" src="http://localhost:5000/userLocation.jsp" nameList="userId" />
<property name="inputmodes" value="dtmf"/>
<menu id="menuZero">
<choice dtmf="1" next="#choice1"/>
<choice dtmf="2" next="#choice2"/>
</menu>
<!--/form-->
<form id="choice1">
<block>
<if cond="userData.HttpResponse.do_queryResponse[&apos;return&apos;].errorMsg.result_code != &apos;0&apos;">
<goto next="welcome.vxml"/>
<else/>
<goto next="welcome.vxml"/>
</if>
</block>
</form>
<form id="choice2">
<block>
<log expr="JSON.stringify(userData.HttpResponse)"/>
</block>
</form>
</vxml>
Maybe, VoiceXML is not supported "JSON.stringify".
Try to get "json2.js" and add code.
<script src="json2.js" />
For example,
<?xml version="1.0" encoding="UTF-8"?>
<vxml
version="2.0"
xmlns="http://www.w3.org/2001/vxml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<script src="json2.js" />
<var name="messageObject" expr="{keyA:'valueA',keyB:'valueB',keyC:'valueC'}" />
<form>
<block><prompt>Write Log!</prompt></block>
<block><log expr="JSON.stringify(messageObject)"/></block>
</form>
</vxml>
I tested this code on "Voxeo Prophecy 13".
I tried json2.js as described above and I had the same issue, 'assignment to undeclared variable JSON'. To fix this, I just declared in the same file (json2.js):
var JSON;
Then it worked properly. In the vxml:
<script><![CDATA[
prueba = new Object();
prueba.pepito = 1234;
prueba.OtraPrueba = "lalalapepe";
]]></script>
<log label="IVB_HISTORY">
<value expr="JSON.stringify(prueba)"/>
</log>
It gets logged as followed:
{"pepito":1234,"OtraPrueba":"lalalapepe"}
I'm using Convergy's InVision Studio

How to set advanced data grid column from a xternal xml?

I have a xml file
<Column id="first name" headerLabel="first name" dataField="first name" />
<Column id="regionName" headerLabel="Region" dataField="region_name" />"
<Column id="lastname" headerLabel="last_name" dataField="last_name"/>
I want this column name to be assigned to my data grid as i am succesfull in picking up data from a back end and save in a variable now i just want to display it in my advanced data grid can any body guide me how to do that with a written example?
<mx:AdvancedDataGrid
id="list"
dataProvider="{data}"
columns = ?/>
I am using this code supoose my xml file name is in assets/config/rumpy.xml from which i want to retrieve column name using header field.
I would suggest you to convert this XML to a XMLList and add columns to the data grid while iterating through its elements.
By default you should determine an empty set of data grid columns. In my example if you don't do it, you will get two times more columns, because the grid can define columns automatically according to the data provider's structure.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.events.FlexEvent;
private var columnList:XMLList;
[Bindable]private var data:ArrayCollection = new ArrayCollection([
{first_name:"John", last_name:"Busch", region_name:"CA"},
{first_name:"Mike", last_name:"Harris", region_name:"DC"},
{first_name:"Linda", last_name:"Brucks", region_name:"CA"}]);
protected function init(event:FlexEvent):void
{
loadXML();
}
private function loadXML():void
{
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onXMLComplete);
loader.load(new URLRequest("assets/config/rumpy.xml"));
}
private function onXMLComplete(evt:Event):void
{
var xmlData:XML = XML((evt.currentTarget as URLLoader).data);
columnList = xmlData.Column;
var cols:Array = list.columns;
for each (var item:XML in columnList)
{
var adgc:AdvancedDataGridColumn = new AdvancedDataGridColumn(item.#dataField);
adgc.headerText = item.#headerText;
cols.push(adgc);
}
list.columns = cols;
}
]]>
</fx:Script>
<mx:AdvancedDataGrid id="list" x="10" y="10" dataProvider="{data}" width="360" height="120">
<mx:columns/>
</mx:AdvancedDataGrid>
</s:Application>
//rumpy.xml
<Columns>
<Column id="first_name" headerText="First Name" dataField="first_name"/>
<Column id="regionName" headerText="Region" dataField="region_name"/>
<Column id="lastname" headerText="Last Name" dataField="last_name"/>
</Columns>

autorun application in flex

I found an application that read the information about you webcam.
The thing is that when the application start you need to click on it to view your stats.
I have now connected the application to a button that animate the mx:list i created, But anyhow I need to fist click on the list to generate the output on a mx:label.
Any Ideas how I should do to just click on my show button to autorun camera stats.
Thanks
<?xml version="1.0"?>
<!-- behaviors\CompositeEffects.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.utils.ObjectUtil;
private var camera:Camera;
private function list_change(evt:ListEvent):void {
var tList:List = evt.currentTarget as List;
var cameraName:String = tList.selectedIndex.toString();
camera = Camera.getCamera(cameraName);
textArea.text = ObjectUtil.toString(camera);
}
]]>
</mx:Script>
<mx:Parallel id="ZoomRotateShow">
<mx:Zoom id="myZoomShow"
zoomHeightFrom="0.0"
zoomWidthFrom="0.0"
zoomHeightTo="1.0"
zoomWidthTo="1.0"
/>
<mx:Rotate id="myRotateShow"/>
</mx:Parallel>
<mx:Sequence id="ZoomRotateHide">
<mx:Rotate id="myRotateHide"/>
<mx:Zoom id="myZoomHide"
zoomHeightFrom="1.0"
zoomWidthFrom="1.0"
zoomHeightTo="0.0"
zoomWidthTo="0.0"
/>
</mx:Sequence>
<mx:List id="list"
dataProvider="{Camera.names}"
width="200"
change="list_change(event);"
hideEffect="{ZoomRotateHide}"
showEffect="{ZoomRotateShow}" visible="false"
/>
<mx:Label id="textArea" height="185" x="674" y="228" dataChange="{Camera.names}" />
<mx:Button id="myButton1"
label="Show!"
click="list.visible=true;"
/>
<mx:Button id="myButton2"
label="Hide!"
click="list.visible=false;"
/>
</mx:Application>
First, I split your function in two:
private function list_change(evt:ListEvent):void {
var tList:List = evt.currentTarget as List;
populateCameraInfo(tList.selectedIndex.toString());
}
private function populateCameraInfo(cameraName:String):void {
camera = Camera.getCamera(cameraName);
textArea.text = ObjectUtil.toString(camera);
}
Next (and I'm assuming you want the list to be visible to start with):
<mx:List id="list"
dataProvider="{Camera.names}"
width="200"
change="list_change(event);"
hideEffect="{ZoomRotateHide}"
showEffect="{ZoomRotateShow}"
selectedIndex="0"
creationComplete="populateCameraInfo('0')"
/>
So that way the first item is always selected.

Flex 4 / ILOG Elixir -- I need help adding dynamic series to the Elixir Radar Chart

I'm having a real hard time adding RadarColumnSeries to my Radar Chart using ActionScript. I've been messing with this for so long that I'm not sure I'm even on the right track anymore. Help PLEASE, Thank you --
Here's what I'm working with in Flex4:
Chart:
<ilog:RadarChart id="newChart" width="100%" height="100%"
showDataTips="true" dataProvider="{xmlDataSource}"
type="circular"
columnWidthRatio="0.8" >
<ilog:angularAxis>
<ilog:AngularAxis dataProvider="{months}" displayName="Month" categoryField="fullDate" />
</ilog:angularAxis>
</ilog:RadarChart>
Months Array:
[January 2011, February 2011, March 2011, April 2011]
xmlDataSource:
<root>
<series id="Americas" name="Americas">
<item total="2" year="2011" month="April" fullDate="April 2011"/>
<item total="3" year="2011" month="February" fullDate="February 2011"/>
<item total="2" year="2011" month="March" fullDate="March 2011"/>
</series>
<series id="Asia-Pacific" name="Asia-Pacific">
<item total="2" year="2011" month="April" fullDate="April 2011"/>
<item total="1" year="2011" month="January" fullDate="January 2011"/>
<item total="1" year="2011" month="March" fullDate="March 2011"/>
</series>
<series id="EMEIA" name="EMEIA">
<item total="1" year="2011" month="April" fullDate="April 2011"/>
<item total="1" year="2011" month="February" fullDate="February 2011"/>
<item total="3" year="2011" month="March" fullDate="March 2011"/>
</series>
<series id="Global" name="Global">
<item total="3" year="2011" month="April" fullDate="April 2011"/>
<item total="3" year="2011" month="March" fullDate="March 2011"/>
</series>
</root>
ActionScript:
private function createSeries():void {
var s:RadarSeries;
for each (var x:XML in xmlDataSource.series) {
var o:Object=new Object();
s = new RadarColumnSeries();
for each (var c:XML in x.item) {
s = new RadarColumnSeries();
//trace(x.#name + ": " + c.#total + " - " + c.#month + " - " + c.#year + " - " + c.#fullDate);
o = {name:x.#name, total:c.#total.toString(), month:c.#month, year:c.#year, fullDate:c.#fullDate};
dataSource.addItem(o);
s.dataProvider = o;
s.dataField = "total";
s.displayName = o.name;
}
columnSeries.push(s);
RadarColumnSeries(s).columnWidthRatio=0.80;
}
newChart.series = columnSeries;
}
Here's a link to the solution

How to set flex 3 tree label clickable?

How to set tree label clickable so tree node can expand? (not only click on arrow icon)
Solved.
<mx:Script>
<![CDATA[
import mx.collections.ICollectionView;
import mx.events.ListEvent;
private function tree_itemClick(evt:ListEvent):void {
var item:Object = Tree(evt.currentTarget).selectedItem;
if (tree.dataDescriptor.isBranch(item)) {
tree.expandItem(item, !tree.isItemOpen(item), true);
}
}
]]>
<mx:XML id="dp">
<root>
<folder label="One">
<folder label="One.A">
<item label="One.A.1" />
<item label="One.A.2" />
<item label="One.A.3" />
<item label="One.A.4" />
<item label="One.A.5" />
</folder>
<item label="One.1" />
<item label="One.2" />
</folder>
</root>
</mx:XML>
<mx:Tree id="tree" dataProvider="{dp}" showRoot="false" labelField="#label" width="300" rowCount="6"
itemClick="tree_itemClick(event);" />