weblog . Biography . Flexibility . projects
  • photography by Kristel van Beek

    Opening Flash documents within Flex
    Flex Builder for Linux

    27 Sep: Testing the existance of an attribute in XML with AS3

    posted by: martijn, at 27 September 2007 11:46 GMT+1, 13 August 2009 23:28 GMT+1

    I had the need to test the existance of an attribute in a XML node. Searching the internet didn't resolve much and tracing a non existing attribute gave me no result
    trace(node.@test)
    

    Testing the result as a string wouldn't help because the attribute is of the type XML.

    My solution is not elegant but it works (I'm hoping someone can tell me a better solution).

    if ( node.@test != node.@nonexistingattribute ) {
        // attribute exists
    }
    

    As you can see I compare the attribute with an attribute I definitely know is not present.

     

    Update: The technique given by Mathis is working like a charm, thanks!

    Update 2: My apologise, the method Mathis has given doesn't work. The one I found working is:

    if ("@attr" in node) { ... }
    

    Thanks Valery Silaev

    Add this link to del.icio.us vote in HexoSearch permalink - tags: flex - 12 Comments
    Write a comment for this article

    12 Comments

    • Eric-Paul says:

      at 4 December, 2007 11:12 www.lostboys.nl
      Martijn, thanks for this post. Even a month later this is the only post I could google about the topic! I found the solution Robert gave looks somewhat "cleaner" in code, without the quotes of the if ("@attr" in node) ... But the jury is still out on the best way.
    • Robert Penner says:

      at 27 September, 2007 22:42 robertpenner.com
      Yet another way is if (node.@attr.length())
    • Andres Santos Flex says:

      at 27 September, 2007 20:59 www.asb-labs.com/blog
      Hey hi Martijn, yep i had that problem as well i used this fancy strategy but as you said it works! if(node.@atribute.toString().length>0){ //code } not the best solution but it is working on my mxml decompiler (posted on my blog) www.asb-labs.com/blog
    • Valery Silaev says:

      at 27 September, 2007 15:10 flexblog.faratasystems.com
      This might work: if ("@attr" in node) { ... }
    • mathis says:

      at 27 September, 2007 14:01 mathis.komplex3.de
      what about this fairly simply strategy: if ( node.hasOwnProperty('@test')){ // attribute qtest exists }
    • What? says:

      at 27 September, 2007 13:50
      How about if (node.@attr==undefined) { doesn't exist }
    • Haungo says:

      at 8 March, 2008 20:42 haungo.net
      I'm very glad I found this page... The following worked perfectly for me:if (node.@attr==undefined) { doesn't exist }Thanks!
    • starpause says:

      at 21 April, 2008 23:08 starpause.com/
      i'm with haungo, testing the @attribute against undefined does the trick!
    • Neil says:

      at 8 October, 2008 13:20
      I use the hasOwnProperty() method to check it exists, eg; hasOwnProperty("@type") this.content_xml.item.(hasOwnProperty("@type") && @type=="group"); will give me all items that actually have type and are of type group. Neil
    • Fridasongs says:

      at 30 March, 2009 13:31
      I was very frustated withthis issue after migrating from as2 to as3.Thanks for the help.This works fine (as pointed out by Whats?) :if (node.@attr==undefined) { doesn't exist }Wanted to point out it also works in checking for child nodes.if (node.childnode==undefined) { doesn't exist } 
    • bLb says:

      at 15 May, 2009 6:36
      if(node.attribute("attr")) { // attribute exists } works like a charm :-)BTW, to change the attribute with this method: node.attribute("attr")[0] = "new value" ;I searched 3 hours to find that last one out :-)
    • cafeine says:

      at 13 November, 2009 14:18 www.cncstudios.gr
      very helpful....works like a charm!
    Write a comment for this article
  • categories
    • flash (26)
    • flex (26)
    • flash player (10)
    • php (7)
    • adobe (6)
    • eclipse (5)
    • weblog (5)
    • open source (4)
    • red5 (4)
    • mac (4)
    • linux (4)
    • apple (4)
    • style (4)
    • windows (3)
    • air (3)
    • apollo (3)
    • javascript (3)
    • design (3)
    • firefox (3)
    • flash media server (3)
    • uimodifier (2)
    • silverlight (2)
    • support (2)
    • microsoft (2)
    • bitmap limits (2)
    • internet explorer (2)
    • safari (1)
    • team (1)
    • chrome (1)
    • devigner (1)
    • git (1)
    • flex builder (1)
    • opera (1)
    • mxna (1)
    • google (1)
    • msn (1)
    • internet provider (1)
    • dell (1)
    • advertisement (1)
    • developer (1)
    • repent (1)
    • nintendo wii (1)
    • java (1)
    • music (1)
    show all
  • all articles
    • February, 2010
    • Flash on Mac Faster
    • December, 2009
    • Historic moment (somehow), Microsoft names Devigner in an article.
    • Universal filesystem
    • November, 2009
    • Projects get a home
    • October, 2009
    • I'm a daddy from now on, her name is Julia
    • My projects moved to GitHub
    • SWFLoader fails where Loader succeeds
    • September, 2009
    • Note to self: Ant Java heap space on Mac OS
    • August, 2009
    • Flash Player Bitmap Limits Again
    • May, 2009
    • Sloppy code
    • April, 2009
    • Flash CS4 IDE navigateToURL bug on the Mac
    • Flex Builder Gumbo expired
    • March, 2009
    • Dear Steve II: The files problem
    • Dear Steve I: The support problem
    • January, 2009
    • Flash Player Bitmap Limits
    • December, 2008
    • Google Chrome and why I use it for Flex debugging
    • November, 2008
    • UIModifier, release 0.0.1
    • October, 2008
    • AIR and Icons
    • Matrix rotation
    • September, 2008
    • CS4 announced, still huge price differences
    • Dear Adobe
    • Browser (technology) war
    • July, 2008
    • mx.effects.Move is Evil
    • Adobe, why no 64-bits Flash Player?
    • Collaboration with Adobe Flex
    • June, 2008
    • My vision on the web video future
    • Flex Builder rename bug (?)
    • May, 2008
    • Flash TextField bug?
    • Astro, Flash Player 10 prerelease has arrived
    • April, 2008
    • Released new website for Kristel van Beek
    • Portishead
    • March, 2008
    • IE7 + HTTPS + Flash
    • Mac experiences
    • I'm a mac now
    • February, 2008
    • AIR, my opinion!
    • Ubuntu living under Windows
    • Pulse, Eclipse on demand
    • January, 2008
    • Zend Neon becomes Zend Studio for Eclipse
    • BlueBerry needs Developers
    • Sparkling new website
    • December, 2007
    • Super Mario Galaxy
    • Apple, please deliver a 15" MacBook Pro with 1920x1200 resolution
    • November, 2007
    • Buzzword, amazing...
    • Garbage collection of the Flash Player
    • October, 2007
    • Flash Player memory differences per browser
    • Zend Neon
    • Flex Builder for Linux
    • September, 2007
    • Testing the existance of an attribute in XML with AS3
    • Opening Flash documents within Flex
    • PDT 1.0 Released
    • EU vs Microsoft
    • August, 2007
    • Repent has a brand new website
    • Manipulating XML with namespace in AS3
    • What's going on with MXNA?
    • Red5 Admin Panel's future
    • July, 2007
    • Red5 Admin Panel 0.2 release (update)
    • June, 2007
    • Red5 Admin Panel 0.1
    • BabbaVideo: A new dutch Video site
    • Safari on Windows
    • Adobe Flex 3 and AIR
    • Adobe Live 2007
    • May, 2007
    • Tommorow Adobe Live
    • Strange Windows bug
    • April, 2007
    • The dark side of Flash
    • March, 2007
    • BlueBerry needs a Flex Developer
    • Apollo server, hmm what is that
    • February, 2007
    • Why (especially Adobe) and other companies should support Linux
    • Flex/Eclipse plugin idea
    • January, 2007
    • PHPEclipse is out, welcome PDT
    • Linux Flash Player 9 released
    • I Welcome all you MXNA readers
    • FullScreen and wMode
    • FLV Player in Flex
    • December, 2006
    • Dell Support rocks
    • November, 2006
    • Flash Media Server 3, and more
    • October, 2006
    • XS4ALL trouble
    • BlueChat, Flash Media Server Application
    • September, 2006
    • Flex Google calender
    • Skinning v2 components
    • Flash Media Server Application and advanced skinning
    • August, 2006
    • I finally discovered JSFL
    • You know sIFR?
    • PHP IDE
    • Datagrid experiences
    • July, 2006
    • My weblog is born
    show all
All rights reserved | Design and Development by Martijn van Beek | Managed by Flexibility | Photography by Kristel van Beek

You have Flash Player version: (click to check if you got the latest)
Adobe feeds | LinkedIn profile | Last.fm profile | Github