PDA

View Full Version : xml Parsing help


mfsiddiq
05-22-2008, 02:06 PM
I am having problems parsing xml node.My xml node is as follows
<data>
<object>
<formItem>
<label>Dish</label>
<value>sweet</value>
<value>Spicy</value>
<controlType>RadioButton</controlType>
</formItem>
</object>
</data>

Now based on the number of <value> tag present i need to create radio buttons on the fly.May i know how to parse the following xml to create 2 radio buttons with labels sweet and spicy

tarafenton
05-22-2008, 02:15 PM
how far are you with parsing the xml? you need to post your AS code too.
if you want to watch an excellent video on xml basics check out this video ActionScript 3 XML Basics on this site http://www.gotoandlearn.com/

mfsiddiq
05-22-2008, 02:25 PM
for each (var n:XML in externalXML.displayObject.formItem)

this is how i m parsing the xml.After which i am using using IF statement

like this if(n.controlType=='RadioButton')
{
//do this
Here i need to add the code for creating radio Buttons
}

tarafenton
05-22-2008, 02:44 PM
you want to first extract the value for each radio option then you can add in the radio button. I don't use the flash component for radio buttons (i create my own movie clip with two frame labels, selected and unselected) and I've never created one from xml, so I would say someone else should jump in, I can help if you are making your movie clips of the radio buttons

this is just a push in the right direction, I'm not sure exactly.

for each (var n:XML in externalXML.displayObject.formItem)
//your code
if(n.controlType=='RadioButton')
{
//Here you need to add the code for creating radio Buttons
for each (var radio:XML in externalXML.displayObject.formItem.value)
//first get the values
trace(externalXML.displayObject.formItem.value);
//then add the radio button
var myRadio:MovieClip = new MovieClip();
var myRadioName = externalXML.displayObject.formItem.value;
myRadio.name = myRadioName;
myRadio.gotoAndStop.myRadio("unselected");
addChild(myRadio);
}
}

mfsiddiq
05-22-2008, 03:03 PM
Thanks for your help.But its not working the way i wanted.My code was like this
if(n.controlType=='RadioButton')

{

var count:Number=0;

var newObjectContainer:FormItem = new FormItem();

newObjectContainer.direction='horizontal';
newObjectContainer.label = n.label;

var radioGroup:RadioButtonGroup=new RadioButtonGroup();
for(var i:int=0;i<n.childIndex();i++) {
if(n.hasOwnProperty('value'))
{
var radio:RadioButton=new RadioButton();
radio.group=radioGroup;
radio.label=n.value[i]
newObjectContainer.addChild(radio);
}
}
registrationForm.addChild(newObjectContainer);
}
although it works fine.I know it is not the right way.coz the for loop is not coded properly if anyone can shed some light on how to code the For loop it will be great.