Problem with XML attributes in AS3.0
Tuesday, August 18th, 2009Table of contents for flex-tips
- Problem with XML attributes in AS3.0
- Unable to export SWC oem
- Numeric sorting of Flex DataGridColumn
I’ve found today some small problem with setting XML attribute for my current application. I’m generating HTML source code via XML, because both are tags languages and I have found this nice problem. Imagine you have DIV tag with class attribute set like this:
<div class="myClass">test</div>
So in ActionScript 3 you can create it via XML in this way
var divXML:XML = <div>test</test>; divXML.@class = "myClass";
All seems ok and trivial, but Flex Builder has problem to compile second line. It seems that @class is reserved word, because I can change to other attribute name and it works. But luckily there are different ways how to set XML attribute for XML node. I’ve fixed it in this way
var divXML:XML = <div>test</test>; divXML.@["class"] = "myClass";
What is your preffered way to set XML attributes?






7 Comments
Alex Bustin
• Visit Site
August 18th, 2009
I like setting attributes inline. If the XML block to to be made into a child, I’ll use appendChild on the parent XML.
var className:String = “myClass”;
var divXML:XML = <div class={className}>test</div>;
patrick
• Visit Site
August 18th, 2009
surely, this would break in XML also for example
<xtag class=”blergh”>
</xtag>
not just HTML?
because they are both the same. sorry, its just I got the picture from your post that this only breaks in HTML and Flex not XML and Flex.
patrick
• Visit Site
August 18th, 2009
Oh, sorry, my preffered way is the second using the array/string notation
admin
• Visit Site
August 18th, 2009
Btw. Empty tag is another issue I’m experiencing when generating HTML from XML in AS3.0. I will create new post about this, but in short term. If you have empty div and it is written in way
it has screwed up all HTML formating (not just DIV, other tags as well, at least I have tried <STRONG/> as well ), but when it’s written as <div></div> it’s just ok.You can see example in this comment, all after <STRONG/> is bolded, which is really awkward…
patrick
• Visit Site
August 19th, 2009
I think the problem with closed tags like <strong/> instead of <strong></strong> is that xml doesn’t support that so trying to do in xml just breaks it. I think the solution would be to find the source of the XML class, and edit to support the full HTML spec.
chepas
• Visit Site
November 12th, 2009
umm..
dont you think that the simple fact that your html is screwed up,
test looks nicer than test, dont you think ?
shean
• Visit Site
November 12th, 2009
ummm… could also be because the html is wack too:
looks a lot better than
dont you think ?
Live Preview
Leave a comment