5 Replies Latest reply on May 28, 2007 7:28 PM by codeworm

    Referencing attributes in a Flex app

      I am a j2ee programmer and newbie to the Apollo/Flex world. I have created a simple application that reads an XML using a HTTPService. The problem I am facing is that I am not successful in referencing attributes in the XML response.

      My XML response is something like this:

      <level1 attr1="value1" attr2="value2">
      <level2 attr3="value3" attr4="value4">SOMETHING</level2>

      I am able to pull out and display "SOMETHING" in the Apollo/Flex app using

      But I am not able to retrieve and display "value1", "value2", "value3" and "value4".
      What should be the XML notation to reference the values of these attributes? Should my result format be changed (I tried "text", "e4x" and "xml")?

      I tried using the "@" (like this text="{myHTTPRequest.lastResult.level1.@attr1}") but this doesn't seem to work? Can anyone please help?

      I posted this in Apollo forum, but was not resolved.

      Thanks in advance.

        • 1. Referencing attributes in a Flex app
          BLXWebMaster Level 1
          Here is a quick example of a HTTPService to capture an XML file. I created a simple Alert to show one way to access the XML element you are trying to get. Obvioulsy there are many ways to traverse XML, but this should get you started.

          If you are still confused, try posting your actual code, and I can modfiy it.

          <?xml version="1.0" encoding="utf-8"?>
          xmlns:mx=" http://www.adobe.com/2006/mxml"

          import mx.collections.*;
          import mx.controls.*;
          import mx.rpc.events.*;
          import mx.events.*;

          private var customers:ArrayCollection = null;
          private function customersRPCResult(event:ResultEvent):void {
          customers = new ArrayCollection();
          customers = event.result.customers.person as ArrayCollection;


          // Fault handler - displays the error
          private function customersRPCFaultHandler(event:FaultEvent):void
          Alert.show(event.fault.message, "Could not load customers.php XML");


          • 2. Re: Referencing attributes in a Flex app
            codeworm Level 1
            BLXWebMaster, Thanks for your wonderful example.

            In your example, the line:
            ...displays the value in the tag called "first". Is that right?

            For example, I assume your xml to look something like this:

            If your example would look like this (I have included attributes),
            <first name="henry" age="25" zip="86001">somevalue</first>
            <first name="bill" age="40" zip="85021">someothervalue</first>

            Can you please let me know how you would be able to retrieve the name, age and zip alone?

            • 3. Re: Referencing attributes in a Flex app
              ntsiii Level 3
              I advise using resultFormat="e4x". The default is "object", which attermpts to convert the xml into an nested object structure.

              Situations like you describe is the main reason I don't like letting the Flex black box convert my xml into some object structure.

              If you use e4x, you access attributes like this:
              var sAttrValue:String = myXML.myNode.@myAttribute;
              of from your last example:
              var sName:String = "henry";
              var xmlNode:XML = myXML.person.first.(@name==sName);
              var sZip:String = xmlNode.@zip;

              Also, do not bind directly to lastResult, it is too hard to debug. Instead, do as BLXWebmaster shows, use result and fault handlers.

              • 4. Re: Referencing attributes in a Flex app
                BLXWebMaster Level 1
                Obviously you will need to loop thought and change the index, but this is a way to access your XML via dot notation.

                Alert.show("First Name: " + event.result.customers.person[0].first.name);
                Alert.show("Age: " + event.result.customers.person[0].first.age);
                Alert.show("Zip: " + event.result.customers.person[0].first.zip);
                • 5. Referencing attributes in a Flex app
                  codeworm Level 1
                  Thanks a lot BLXWebMaster and ntsiii.
                  Got it working now.

                  One more question. In this same methodology, is there a way to retrieve the attributes of the root tag? The notation gets the list and starts from the first child node and doesn't give pointer to the root tag.
                  <customers count="2" demographic="az">
                  <first name="henry" age="25" zip="86001">somevalue</first>
                  <first name="bill" age="40" zip="85021">someothervalue</first>

                  Is there a way to retrieve "count" and "demographic"?

                  Again, thanks a lot for helping out.
                  Pradeep Peethambaram