Solved

How do I assign xml attributes

Posted on 2011-02-21
16
860 Views
Last Modified: 2012-05-11
How do I set the quotes in my xml-string to create attributes.
In the example below the id in the brackets will simply show as is, not as the actual id-number.
newfile = <item id="{id}"><name>{xmlsetName}</name><age>{xmlAge}</age></item>;

Open in new window

How do I set the quotes correct.
Something like
<item id=+"{id}"+>

Open in new window

0
Comment
Question by:petersego
  • 8
  • 8
16 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34942408
newfile = "<item id='" + id + "'><name>" + xmlsetName + "</name><age>" + xmlAge + "</age></item>";

Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34942880
Thanks, but I would prefer to keep my syntax - not makiing it a string..
It works fine as long as I only use nodes and is going to be send through php.
What Im looking for is the right syntax """{id}"""
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34943106
In Flash the bindlable {} will not work, its for Flex I guess.
Hence then try creating a XMLNode and using appendChild and adding attributes to the nodes.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34943244
Oh yes, it works. Originally I had the id in a node, and that worked flawlessly.
newfile = <item><id>{id}</id><name>{xmlsetName}</name><age>{xmlAge}</age></item>; 

Open in new window

but since I want to catch the corresponding nodes of the id, I assumed it would be better to place it in an attribute of the parentnode item.
What Im trying to do is to check if an id-number is in an xml-file. If it is, I want the corresponding name and age placed in each a dynamic textfield.
else I want to generate an id-number and place som input-textfields.
And thats where newfile is relevant.
My problem is that I cant get the if else work.
if the "id" doesnt exist, the else-part isnt executed.

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34943300
That is really cool that you can use Bindable parenthesis {} in Flash, please may I know which version are you using in Flash IDE?
And can you share your code snippet of where you used the if else condition.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34943680
Well, Im still on CS4.
I might return with the if else problem. Something is definitively wrong...
But if you would check it, I'd be gratefull.
myXML = XML(myxmlLoader.data);
idLength=myXML.item;
for (var i:int; i < idLength.length(); i++ ) {
if (idLength[i].id==id){
nametxt.text=idLength[i].(id==id).name;
agetxt.text=idLength[i].(id==id).age;
}
else{
nametxt.text="Input your name";
agetxt.text="input your age";
}

Open in new window

and the xml
<?xml version='1.0' encoding='utf-8'?>
<items>
  <item>
    <id>106BB10495</id>
    <name>James</name>
    <age>65</age>
  </item>
  <item>
    <id>9915R72620</id>
    <name>Jerry</name>
    <age>87</age>
  </item>
  <item>
    <id>013R293445</id>
    <name>Jimmy</name>
    <age>22</age>
  </item>
</items>

Open in new window

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34948290
In your comparison:
if (idLength[i].id==id)

Open in new window

where will the variable "id" come from?

Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34949057
id wil come from flashvars.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:petersego
ID: 34949240
It seems that I cant iterate. If is only correct, if the correct id is placed first in the xml.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34949282
Try this:
myXML = XML(myxmlLoader.data);

nametxt.text="Input your name";
agetxt.text="input your age";

nametxt.text = myXML.item.(id == flashVarId).name;
agetxt.text = myXML.item.(id == flashVarId).age;

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34949400
Ahh, okay, I dont have to iterate.
That helped on getting the correct id, nomatter where it was in the xml, but Im still having problems with the else part. If it isnt comparable to any id in the xml, nothing happens.

if(myXML.item.(id == flashVarId)){
nametxt.text = myXML.item.(id == flashVarId).name;
agetxt.text = myXML.item.(id == flashVarId).age;
}
else{
nametxt.text="Input your name";
agetxt.text="input your age";
}

Open in new window

if flashVarId doesnt exist in xml, nothing will show in the textfield.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34949429
If you use my script under ID:34949282, then you will not have to write the else part.
This is because, by default the values are set first, then the actual values are set based on id match.

So if the ids do not match nothing happens and the default values are set.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:petersego
ID: 34949565
Well, in fact Im not only setting new values to the textfield, Im also changing the textfield to inputtype with borders and other input-features like eventlisteners
so in reality it will be looking like this:
var nametxt:TextField=new TextField();
var agetxt:TextField=new TextField();
inputTF();
valuesTF(); //This is if the FlashVarsId is present in the xml
addChild(nametxt);
addChild(agetxt);
which again results in that nothing happens if the FlashVarsId isnt present in the xml
function inputTF(){
	nametxt.x=100;
	nametxt.y=58;
    nametxt.width=190;
	nametxt.height=18;
    nametxt.border=true;
	nametxt.borderColor=0x666666;
	nametxt.selectable=true;
	nametxt.background=true;
	nametxt.type = TextFieldType.INPUT;
	nametxt.text="Input your name";
	nametxt.setTextFormat(preInputFormat);
	nametxt.addEventListener(Event.CHANGE,updatetext);
	nametxt.addEventListener(MouseEvent.CLICK,removeNameText);
	
	agetxt.x=100;
	agetxt.y=83;
	agetxt.width=190;
	agetxt.height=18;
	agetxt.border=true;
	agetxt.borderColor=0x666666;
    agetxt.selectable=true;
	agetxt.background=true;
	agetxt.type = TextFieldType.INPUT;
	agetxt.text="Input your age";
	agetxt.setTextFormat(preInputFormat);
	agetxt.addEventListener(Event.CHANGE,updatetext);
	agetxt.addEventListener(MouseEvent.CLICK,removeAgeText);
}

Open in new window

0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 500 total points
ID: 34949584
So what I understand is that if the id matches, you will change the type to dynamic - right?
then do this:
myXML = XML(myxmlLoader.data);

nametxt.type = TextFieldType.INPUT;
nametxt.text="Input your name";

agetxt.type = TextFieldType.INPUT;
agetxt.text="input your age";

var _name:String = myXML.item.(id == flashVarId).name;
var _age:String = myXML.item.(id == flashVarId).age;
if( _name ){
    nametxt.text = _name;
    nametxt.type = TextFieldType.DYNAMIC;
}
if( _age ){
    agetxt.text = _name;
    agetxt.type = TextFieldType.DYNAMIC;
}

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Closing Comment

by:petersego
ID: 34950011
Thank you really much. I know that the question ended up far from the original, so I really appreciate it.
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34950048
My Pleasure :)

Warm Regards
Deepanjan Das
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now