Problem with XML attributes in AS3.0

Tuesday, August 18th, 2009
Problem with XML attributes in AS3.0

Table of contents for flex-tips

  1. Problem with XML attributes in AS3.0
  2. Unable to export SWC oem
  3. 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?

tagged under:

ABOUT THIS AUTHOR

Get a Trackback link

7 Comments


  1. 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>;


  2. 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.


  3. patrick
    Visit Site
    August 18th, 2009

    Oh, sorry, my preffered way is the second using the array/string notation


  4. 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…


  5. 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.


  6. 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 ?


  7. shean
    Visit Site
    November 12th, 2009

    ummm… could also be because the html is wack too:

    <div>test&lt/div&gt

    looks a lot better than

    <div>test</test>

    dont you think ?

Live Preview

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

CommentLuv Enabled

Images is enhanced with WordPress Lightbox JS by Zeo