5 Replies Latest reply on Oct 17, 2006 12:29 AM by Kolja1987

    Reading data from XML

    Kolja1987 Level 1
      Hello!

      I want my program to read data from external XML file. I know that Flash has an XML class, but I just don't get the way it

      reads and writes data from/to it.

      I have a following XML file (a part of it):

      <?xml version="1.0"?>
      <configuration>
      <soundvolume>66</soundvolume>
      </configuration>

      I have created an XML class:

      xml_data = new XML();
      xml_data.load("config.xml");

      How to make Flash read this value of 66 and add it to some variable?

      By logic, it would seem to be:

      _global.volume = xml_data.configuration.soundvolume.nodeValue();

      But it's not.

      Also, what are commands for writing data to an XML files.

      Help...

      Thanks in advance.
        • 1. Re: Reading data from XML
          Level 7
          Hi!

          Flash can't write to an xml file (or write anything at all for that matter)
          on its own. You'd need some serverside script to do that (.php, .asp, etc).

          As for the processing of the xml file, you might want to read up a bit in
          the help documentation under the xml class. To handle the different nodes in
          an xml file you use childNode , firstChild and such. Attributes in an xml
          file can be reached with childNode
          .attributes.{attributename} (without
          the {} brackets).

          Below is an example copy/paste of a processig code I've got.

          function processXML(XMLObj:XML):Void {
          var thisClass = this;
          thisClass.RawData = XMLObj;
          thisClass.Data = new Object();
          var node = thisClass.RawData.firstChild;
          for (var i = 0; i < node.childNodes.length; i++) {
          thisClass.Data[node.childNodes .nodeName] =
          node.childNodes
          .childNodes[0].nodeValue;
          }
          }

          If you run this on your xml file then you should end up with:
          this.Data[soundvolume] == 66.

          /Jensen/


          "Kolja1987" <webforumsuser@macromedia.com> wrote in message
          news:egu92e$r0f$1@forums.macromedia.com...
          > Hello!
          >
          > I want my program to read data from external XML file. I know that Flash
          > has
          > an XML class, but I just don't get the way it
          >
          > reads and writes data from/to it.
          >
          > I have a following XML file (a part of it):
          >
          > <?xml version="1.0"?>
          > <configuration>
          > <soundvolume>66</soundvolume>
          > </configuration>
          >
          > I have created an XML class:
          >
          > xml_data = new XML();
          > xml_data.load("config.xml");
          >
          > How to make Flash read this value of 66 and add it to some variable?
          >
          > By logic, it would seem to be:
          >
          > _global.volume = xml_data.configuration.soundvolume.nodeValue();
          >
          > But it's not.
          >
          > Also, what are commands for writing data to an XML files.
          >
          > Help...
          >
          > Thanks in advance.
          >


          • 2. Re: Reading data from XML
            Arivu India
            exapmle 1:
            xml_data = new XML();
            xml_data.load("config.xml");
            xml_data.onLoad = function()
            {
            _global.volume = xml_data.firstChild.firstChild.firstChild.nodeValue;
            trace(_global.volume);
            }

            example 2:

            xml_data = new XML("<configuration><soundvolume>66</soundvolume></configuration>");
            _global.volume = xml_data.firstChild.firstChild.firstChild.nodeValue;
            trace(_global.volume);
            • 3. Reading data from XML
              Kolja1987 Level 1
              I read the documentation, but just didn't understand it. I think now I got that.
              But please, are you absolutely sure that Flash cannot write XML files?
              Can it alter anything else, like TXT files?

              The thing is I won't use my program on the net, but it's kind of an offline game. There must be some way to keep the settings when program quits and reload them when it starts again... Is there?
              • 4. Reading data from XML
                shikozel Level 1
                flash can not write to XML or Text file. you will need a third party (asp, php, javascript...) to handle the writing operation.
                if you are using your program as desktop application you can save your setting as cookie using shared object and read this cookie everytime your application starts. look into Shared Object:
                http://www.adobe.com/support/flash/action_scripts/local_shared_object/
                • 5. Re: Reading data from XML
                  Kolja1987 Level 1
                  Well, thank you all. I will try whit these shared objects.

                  Thank you again.