7 Replies Latest reply on Jul 7, 2011 1:12 PM by LilGames

    How do I check if an XML node exists?

    ChiefRocka00 Level 1
      I am returning XML in the following format:

      <root>
      <menuitem1 label="Restaurant">
      <menuitem1 label="Bistro 9" data="PROJECT_12"/>
      <menuitem1 label="Cafe Abir" data="PROJECT_13"/>
      <menuitem1 label="Firehouse Bistro & Books" data="PROJECT_10"/>
      <menuitem1 label="Herbivore" data="PROJECT_3"/>
      <menuitem1 label="Oasis Grill" data="PROJECT_11"/>
      <menuitem1 label="Restaurant 1515" data="PROJECT_9"/>
      </menuitem1>
      <menuitem2 label="Commercial">
      <menuitem2 label="1425 Mendell" data="PROJECT_8"/>
      <menuitem2 label="Corkage" data="PROJECT_22"/>
      <menuitem2 label="Gilman Paul Market" data="PROJECT_19"/>
      <menuitem2 label="Shirtique" data="PROJECT_16"/>
      <menuitem2 label="Touchdown" data="PROJECT_17"/>
      <menuitem2 label="Ultra-Spective" data="PROJECT_18"/>
      </menuitem2>
      </root>

      In my code I am handling the XML that was returned to me and doing something different for each hi-level menuitem. There is a possibility a menuitem3 could be returned and I need to code that into my Flex app but I am having a heck of a time trying to figure out if it exists. I convert my result set to an XMLList by doing, var xmlMenuitems = XML(event.result);. Then what I am trying to do is something like:

      if ( xmlMenuitems.menuitem3 != null )

      but I am getting an error when it doesn't exist. What is the proper syntax that I need to include here to see if menuitem3 exists?

      Thanks
        • 1. Re: How do I check if an XML node exists?
          ntsiii Level 3
          First a minor correction: the below returns XML, not XMLList.
          var xmlMenuitems = XML(event.result);

          I am surprised that this below errors:
          if ( xmlMenuitems.menuitem3 != null )
          unless xmlMenuItems itself is null. Verify that first:
          trace(xmlMenuitems.toXMLString() ); //if it errors the var is null

          You can also do:
          if ( xmlMenuitems.menuitem3 )

          Finally, since all expressions return XMLList, you can do:
          if ( xmlMenuitems.menuitem3.length() > 0 )

          Tracy
          • 2. Re: How do I check if an XML node exists?
            ChiefRocka00 Level 1
            Thank You Tracy for the many different options for me to try, I will give them a whirl to see what will work for me.
            • 3. Re: How do I check if an XML node exists?
              levancho Level 3
              you can also do xmlMenuitems.hasOwnProperty("menuitem3")
              • 4. Re: How do I check if an XML node exists?
                rmeador
                I'm really new at flex, so take my advice with a large grain of salt. From personal observation (I can't find this mentioned in any docs), it seems that you can request nodes and attributes using E4X notation as much as you want and it will just keep returning empty XML (basically empty strings). Thus checking if it is null will not work. You can do a toString() on it and check it against the empty string, but that won't help you in the case of an attribute that you want to know if it exists and an empty string might be a valid value for it. Following the advice on some blog I found (I can't find the link now, sorry), I tried testing all my XML stuff against "undefined". It's much like null, except that it seems to work :P It is the only method I have discovered that will allow me to test for the existence of an attribute. I also make pretty heavy use of the hasComplexContent() function to check to see if there are sub-tags inside a tag. With these two combined, my XML processing is both easy to read and works great.

                Example:
                if (xmlMenuitems.menuitem3 != undefined) { ... }

                By the way, I noticed your example has menuitem1 objects nested inside menuitem1 objects, and likewise for menuitem2. I belive (though, again, not sure) that E4X will return all of the nodes (nested or not) with a given name when you use the dot notation to access it. Something to keep in mind (if it's true).
                • 5. Re: How do I check if an XML node exists?
                  levancho Level 3
                  I completely agree to rmeador about "undefined" check,
                  then how does this work if xmlMenuitems.menuitem3 is undefined

                  xmlMenuitems.menuitem3 != undefined // false
                  xmlMenuitems.menuitem3 != null // true

                  even though documentation says null==undefined yelds to true,


                  • 6. Re: How do I check if an XML node exists?
                    ntsiii Level 3
                    This is important: ALL E4X EXPRESSIONS RETURN AN XMLList.

                    So an expression will *never* be null. If it finds no nodes, it will simply have length() == 0.

                    An XML node on the other hand can be null.

                    Tracy
                    • 7. Re: How do I check if an XML node exists?
                      LilGames Level 1

                      I use this to test if a node exists

                       

                      if(myXML.node1.node2.toXMLString() != ""){ // do something }

                       

                       

                      If node2 doesn't exist, it converts to "" string