5 Replies Latest reply on Oct 31, 2006 11:15 PM by Ghan27

    Writing to a XML file.

    Ghan27
      Okay boys and girls,

      I'm making a simple app so i can see my work schedule through the internet.
      Now i got it to work with an XML file that populates a datagrid.
      The thing i can't figure out is how i can add new nodes using some text input fields.
      So i need to grab the text from thos fields and then append them into my existing XML file.
      Please help me!!!

      Greetz
      Ghan
      PS. the app is located here http://members.chello.nl/f.hoekstra01
        • 1. Writing to a XML file.
          deepa subramaniam (adobe) Level 2
          Loading an XML file into an mx:XML tag is just like declaring the XML directly in the main mxml file. You can use the methods available on the XML class in the Flash Player to append new nodes, modify existing nodes, delete nodes, etc.

          Here's an example - the test.xml file looks like this:

          <?xml version='1.0' encoding='utf-8'?>
          <node label='root'>
          <node label='child A' />
          <node label='child B' />
          <node label='child C' />
          <node label='child D' />
          <node label='child E' />
          </node>

          Now I append new nodes based on whats entered into the textinput.

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

          <mx:XML id='source' source='test.xml' />

          <mx:TextInput id='ti' text='new node' />

          <mx:Button click='addNewNode();' />

          <mx:Script><![CDATA[

          private function addNewNode():void
          {
          var node:XML = <node />
          node.@label = ti.text;
          source.appendChild(node);
          trace('source.toXMLString(): ' + source.toXMLString());
          }

          ]]></mx:Script>

          </mx:Application>

          HTH -

          Deepa Subramaniam
          Flex Framework Engr
          • 2. Re: Writing to a XML file.
            ntsiii Level 3
            I have a full example that loads an xml file, allows you to edit it, and then save it back to the server.

            http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=544

            Tracy
            • 3. Writing to a XML file.
              Ghan27 Level 1
              Well... got it to work with the trace statement... but the problem is.. when i stop the app the XML file is still as before. It doesn't save it to the xml file......

              <mx:Script><![CDATA[

              private function addNewNode():void
              {
              var node:XML = <node />
              node.@label = ti.text;
              source.appendChild(node);
              trace('source.toXMLString(): ' + source.toXMLString());
              }

              ]]></mx:Script>


              so if i inspect the trace it works but how do i write it to the xml file???
              • 4. Re: Writing to a XML file.
                ntsiii Level 3
                No, sorry, it is not that easy. That example only shows how to manipulate the xml. It does not address accessing files on the server.

                In fact, source='test.xml' is a compile time operation. The xml is baked into the swf and there is not communication with the server at run time at all.

                You must have some server-side functionality to do this.

                Best dig into my example. It is almost as simple as it gets, especially if you ignore the client-side xml manipulation.

                Tracy
                • 5. Re: Writing to a XML file.
                  Ghan27 Level 1
                  Thnx!
                  it's a bit complicated for just a novice like me but i'll try to get through it!!
                  At least you took the effort to put comments in your code :P

                  Greetz
                  Ghan