8 Replies Latest reply on Jan 5, 2009 1:03 PM by hsfrey

    XML file input and parsing

    hsfrey Level 1
      In looking through the documentation for XML handling, I can find no command for something as basic as how to read in a local XML file for parsing and processing.

      Can someone recommend a site with example code for manipulating XML files?
      I've done some of this before in Java and Perl. Does ActionScript have similar facilities?

        • 1. Re: XML file input and parsing
          Don't think there is access to local file system from Flex app however AIR apps can do this - see the File class


          Once you read in the filestream you can convert it to XML Object and then handle it as you would any XML data source (e4x).
          • 2. Re: XML file input and parsing
            hsfrey Level 1
            Thanks flexstone.
            But that's a Bummer!

            What if I put the XML file on my website, and loaded it from there?
            Or included the XML file in the source code, like a DATA statement?
            Or pre-parse the XML into an AS tree with something like perl, and include that in the source code as part of an AS script.

            Yuck - I'd have to include the program with every data set.
            I had hoped I could avoid a kludge.

            BTW, livesource.adobe.com has been down all day. Fortunately the langref zip could still be downloaded.
            • 3. Re: XML file input and parsing
              Richard_Abbott Level 3
              I often put XML files onto the web server and then access from Flex code. Something like this:

              protected function load_settings_data():void
              data_URL = "./data_settings.xml";
              data_request = new URLRequest(settings_data_URL);
              data_loader = new URLLoader(settings_data_request);
              data_loader.addEventListener("complete", settings_data_loaded);
              data_loader.addEventListener("ioError", settings_data_error);
              } // try
              catch (error:Error)
              Alert.show("load_settings_data - error message " + error.toString());
              } // catch
              } // load_settings_data

              protected function settings_data_error(e:IOErrorEvent):void
              Alert.show("ioError handled in settings_data_error: error " + e.text);
              } // settings_data_error

              protected function settings_data_loaded(event:Event):void
              xml_to_use = XML(data_loader.data);
              // do whatever you want with xml_to_use
              } // settings_data_loaded

              However, while you are developing on the local machine you will probably want to have some dummy data in a variable:
              protected var data_internal:XML =
              and swap this data in in the catch part of the first statement. Obviously the dummy data has to have the right structure but you can usually get away with just a few entries rather than the whole lot. Once you are happy, forget the local variable, put back the error handling and just use the file on your web server which can be updated whenever you want independently of the swf application.

              Hope that helps,
              • 4. Re: XML file input and parsing
                hsfrey Level 1
                Thank You Richard!

                That's just the answer I needed! :-)

                • 5. Re: XML file input and parsing
                  rtalton Level 4
                  Also, check the Flex HTTPService. It can simply retrieve the xml data for you.
                  Like this:
                  <mx:HTTPService id="myDataService" url="data/myData.xml" resultFormat="e4x"/>
                  • 6. Re: XML file input and parsing
                    hsfrey Level 1
                    Great! Looks even easier!
                    And that led me to check out E4X.
                    This whole program may be easier in Flex than I thought it would be!
                    • 7. Re: XML file input and parsing
                      ntsiii Level 3
                      "e4x" is just the current ActionScript implementation of XML(ecmascript for xml). Use it.

                      I also advise getting in the habit of using result handlers instead of directly binding to lastResult.

                      Here are some more snippets.


                      • 8. Re: XML file input and parsing
                        hsfrey Level 1
                        Wow! You guys are good!

                        Your "snippet" deserves a lot of study.