1 Reply Latest reply on Jan 6, 2011 7:26 AM by Gregory Lafrance

    How to identify the XML tag's name

    Joker_82

      Hi,

       

      I think it's a very basic question regarding XMLs, but I'm having troubles with it.

      I'm using a tree element, but it doesn't really matter.

       

      Let's say that one of the nodes in my XML is:

      <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>

      My XML also contains node for files, something like this:

      <FILE ID="456" NAME="MyFile" type="FileType" />

       

      I need to identify whether an item in the XMLList is a file or a folder. I'm using the "type" attribute, but I would like to simply know what kind of tag is it, a FOLDER or a FILE. Currently I'm just doing something like:

       

      if(itemInXMLList[0].@type == "FolderType"){

           //do something related to folders...

      }

       

      I would need to work with a different XML which doesn't containg the "type" attribute.

      How can I know whether it's a FOLDER or a FILE tag?

       

      Thanks.

        • 1. Re: How to identify the XML tag's name
          Gregory Lafrance Level 6

          Just use the XML name() function.

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                private function init():void{
                  for each(var xml:XML in xmllist){
                    if(xml.name()=="FOLDER"){
                      trace("Its a folder.");
                    }else if(xml.name()=="FILE"){
                      trace("Its a file.");            
                    }
                  }
                }
              ]]>
            </mx:Script>
            <mx:XMLList id="xmllist" xmlns="">
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
              <FOLDER ID="123" NAME="MyFolder" type="FolderType"/>
              <FILE ID="456" NAME="MyFile" type="FileType" />
            </mx:XMLList>
          </mx:Application> 
          


          If this post answers your question or helps, please mark it as such. Thanks!

          http://www.stardustsystems.com
          Adobe Flex Development and Support Services