5 Replies Latest reply on Aug 28, 2007 7:47 PM by aceinc

    getItemByAttribute

    aceinc Level 1
      I have the following line of code;

      var menu:XML = XML (AppMenu.dataProvider.getItemByAttribute("label",strParentLabel));

      Flex Builder 2 doesn't have a problem compiling this, and I get no errors when it runs, but it doesn't return any results for a menu dataprovider.

      What I want to do is to search through a menu and find a specific item based on its attribute.

      If someone can tell me what I am doing wrong I would appreciate it.

      Paul
        • 2. Re: getItemByAttribute
          aceinc Level 1
          ellamit,

          I read through the example, and didn't see how it relates.

          I have been playing with things, and what I found was that the dataprovider is an object, not XML, even though I specify "e4x" on my resultFormat tag of the HTTPService that gets the data.

          I think I need to figure out how to get the data back from an HTTPService request in the proper format before I can do what I need to do. All suggestions are welcome.

          Paul
          • 3. getItemByAttribute
            aceinc Level 1
            I think I can get the data in XML format using;

            MenuData = XML(menuLoad.lastResult);

            Once in this format I have been unable to find a way to loop (recursively, I presume) through the children to find the child I need.

            I have looked through documentation, forums, blogs etc. and no one (that I could find) has an example of how to loop through an XML tree. Here is an example that does not work;

            traceNodes(MenuData);

            private function traceNodes(theNode:XML):void
            {
            for(var child in theNode.)
            {
            trace(child.name());
            trace(child.attributes());
            traceNodes(theNode[child]);
            }
            }

            I really need help on this one.

            Paul
            • 4. getItemByAttribute
              levancho Level 3
              you have two seperate issues that are tangled together , lets first seperate them
              1st issue) you need to get correct XML. to do so Alert, or trace yourXMLObject.toXMLString().

              as soon as you have your desired XML object traversing it is trivial .
              here is one example :

              traversing XMLNode of following nature :
              <root>
              <application manager="my.application.manager.insance" >
              <module name="nmyModuleABC" default="yes" type="IApplication" access="ACCESS_ROLE_123" >
              <help id="someID" url="myhelp.xml" />
              </module>
              <module name="anotherModule.swf" type="IModule" access="ACCESS_1234" >
              <help id="anotherId" url="anotherelp.xml" />
              </module>
              </application>
              </root>
              :
              for each (var aModule:XML in this._configurationXML.application.module)
              {

              if((StringUtil.trim(aModule.help.@id)!="")) {
              this.registerModule(StringUtil.trim(aModule.@name),SecurityManager[""+aModule.@access+""] ,StringUtil.trim(aModule.help.@url));
              }
              }

              e4x is very powerfull and fairly simple. if you know XPath is even easier.
              • 5. Re: getItemByAttribute
                aceinc Level 1
                Okay, using help from the posts, I finally wrote a routine that will walk an XML tree. Here is what I wrote;

                private function getXMLByAttribute(xmlInputNode:XML,strNodeName:String,strAttribute:String,strAttributeVal ue:String):XML
                {
                var xmlReturnNode:XML = null;
                for each (var xmlCurrentNode:XML in xmlInputNode[strNodeName])
                {
                if (xmlCurrentNode.@[strAttribute] == strAttributeValue) return xmlCurrentNode;
                xmlReturnNode = getXMLByAttribute(xmlCurrentNode,strNodeName,strAttribute,strAttributeValue);
                if (xmlReturnNode != null) break;
                }
                return xmlReturnNode;
                }

                It will in fact return a node from an xml tree. You can call it like;

                myNode:XML = getXMLByAttribute(myXMLData,"*","myAttribute","myValue");

                the above will return an XML data set containing the first node where the attribute myAttribute is equal to myValue. The * says search all nodes, you can replace it with a string equal to a nodename, like myXMLData.myNodeName.

                But, this doesn't give me what I need (I think). What I need really need is to be able to add a new node as a child below this one, or to delete this node. So what I really need (I think) is a pointer into the original XML dataset suitable to be used in an insert/delete XML method.

                Any Ideas?

                Paul