actionscript-3apache-flexflex4flex3e4x

How to include and use E4x to get XML values in FLex


I'm getting xml response from the servlet in UploadCompleteData() and I need to get the value of XML using E4x in flex. my code is below :

//function where we get xml

function OnCompleteupload(event:Event):void
{
  xml:XML = event.event.currentTarget.data(); // this will give me the Xml 
 //here I need to get the values from xml using E4x
}

// XML response is like

<Students>
  <Student>
      <rollNo>50</rollNo>
      <totalMarks>100</totalMarks>
  </Student>
</Students>

I need to get value somewhat like xml[0].rollNo . I'm a beginner so please give some suggestions and also tell me is there any jar which I need to include in my project in order to use E4X.

Thanks in advance


Solution

  • First, please note that your access URLLoader's data is a property, not a method(). In actionscript 3, unlike java you can have getter and setters that look like public variables from java perspective, but they do call functions behind the scenes.

    So your code should read

    function OnCompleteupload(event:Event):void
    {
      xml:XML = new XML(event.event.currentTarget.data); // this will give me the Xml 
     //here I need to get the values from xml using E4x
    }
    

    The fun thing about E4X is that it's built into the language, so not only you won't need a library (compiled as3 libraries are .swc files, not java .jar) but you can inline xml, so you want to test a quick snippet, this will work:

    var xml:XML = <Students>
      <Student>
          <rollNo>50</rollNo>
          <totalMarks>100</totalMarks>
      </Student>
    </Students>;
    

    Back to your main question, accessing rollNo, this node is nested in the <Student/> node (which is an XMLList...you may have multiple Student nodes later on), which in turn is nested inside the <Student/> node: your root node.

    So going top to bottom, xml.Student will get you to the <Student/> child XMLList, from which you can access the first element, using array access notation: xml.Student[0]. At this point, you drill down one more level to the rollNo node:

    var xml:XML = <Students>
      <Student>
          <rollNo>50</rollNo>
          <totalMarks>100</totalMarks>
      </Student>
    </Students>;
    
    trace(xml.Student[0].rollNo);//prints 50 to the console/output
    

    If you want to learn more, Lee Brimelow has a couple of easy to follow video tutorials on XML and E4X in actionscript 3:

    1. ActionScript 3 XML Basics
    2. ActionScript 3 Advanced XML

    Update Based on your comment, you're dealing with XML Namespaces. The above ActionScript 3 Advanced XML tutorial also covers this.

    You can so something like this:

    var ns:Namespace = new Namespace("urn:xxx:proj:ken");
    var xml:XML = <Students xmlns="urn:xxx:proj:ken"> <Student> <rollNo>20</rollNo> <totalMarks>425</totalMarks> </Student> </Students>;
    //access via namespace
    trace(xml.ns::Student[0].ns::rollNo);
    

    This can get a bit more verbose but it's flexible if you have more XML namespaces in the same document.

    If your XML uses only this one namespace, you can set it as the default XML namespace:

    var ns:Namespace = new Namespace("urn:xxx:proj:ken");
    default xml namespace = ns;
    var xml:XML = <Students xmlns="urn:xxx:proj:ken"> <Student> <rollNo>20</rollNo> <totalMarks>425</totalMarks> </Student> </Students>;
    trace(xml.Student[0].rollNo);//notice you don't need to use ns:: before accessing each node as the default xml namespace is set above