4 Replies Latest reply on Jun 7, 2006 7:54 AM by milostea

    What's the XML syntax for setting a node's value

    milostea
      We have a server application that talks via XML. We have no control on the server's syntax for its XML packets and we would like to talk to it via ActionScript. The problem that we ran into is that the server has a value within a node. This is the snipet of the XML packet that we wish to create:

      <Section name ="DATA">
      <Entry name="command"> 102 </Entry>
      <Entry name="cookie"> 57490 </Entry>
      <Entry name="errorno"> 0 </Entry>
      .
      .
      .
      </Section>

      Now, we can set the attributes named "command", "cookie", etc... but we cannot seem to find the syntax for setting the value for each "Entry" node. Now, it would be cool to use something like this:

      var xmlRecord:XML;
      .
      .
      .
      xmlRecord.Section.Entry.(@name=="command") = 102;

      Unfortunately. this is not a valid statement. Can someone help us understand how we can set the value for a node in ActionScript? Thanks.

      /AC

        • 1. Re: What's the XML syntax for setting a node's value
          Level 7
          Hi milostea,

          > xmlRecord.Section.Entry.(@name=="command") = 102;
          >
          > Unfortunately. this is not a valid statement. Can someone help us understand
          > how we can set the value for a node in ActionScript? Thanks.

          I'm not entirely sure why your version isn't working (I'm checking
          around the E4X experts here to find out), but in the meantime, you can
          try this instead:

          xmlRecord.Section.Entry.(@name=="command").setChildren(XML(102));

          Hope this helps.

          --
          nj
          Flex Builder team
          • 2. Re: What's the XML syntax for setting a node's value
            milostea Level 1
            Thanks. That worked quite well.

            A couple of observations regarding XML in your docs.

            1) Doc has enough examples for reading data but not enough about writing to an XML object. The current examples use explicit writing (inline XML strings) that do not reflect real apps' usage.

            2) Doc needs examples on how to delete nodes from an XML object. I found a post regarding this subject but was not applicable to our situation.

            Thank you.
            • 3. Re: What's the XML syntax for setting a node's value
              ntsiii Level 3
              Did you figure out how to delete a node?

              Amazingly, there is no e4x method, but you have to use AS's delete command.

              Tracy
              • 4. Re: What's the XML syntax for setting a node's value
                milostea Level 1
                Yes. I did. The code below is for a class that uses XML to mimic INI file content by grouping keywords and sections. Here is a sample XML that I have to manipulate:

                <DOC>
                <INISettings>
                <Section name="DATA">
                <Entry name="command">100</Entry>
                </Section>
                <Section name="CLIENT"/>
                <Section name="SERVER"/>
                <Section name="CONNECTION"/>
                <Section name="END"/>
                </INISettings>
                </DOC>

                To delete one of the "Section" nodes I simply do this:

                var indexOf:int;
                var sSection:String;

                sSection = GetSection(_id); //internal function that maps IDs to a string.
                indexOf = 0-1;
                for each( var myvar:XML in m_xmlRoot.INISettings.Section )
                { //Iterate all of the nodes named 'Section' underneath 'INISetting'
                indexOf++;
                if( myvar.@name == sSection )
                {
                delete m_xmlRoot.INISettings.Section[indexOf];
                break;
                }
                }

                To delete a node inside another node, do this:

                var indexOf:int;
                var sSection:String;

                sSection = GetSection(_id); //internal function that maps IDs to a string.
                indexOf = 0-1;
                for each( var myvar:XML in m_xmlRoot.INISettings.Section.(@name==sSection).Entry )
                {//iterate through the nodes named 'Entry' inside Section
                indexOf++;
                if( myvar.@name == _keyword )
                { //found right Entry
                delete m_xmlRoot.INISettings.Section.(@name==sSection).Entry[indexOf];
                break;
                }
                }

                I hope that helps you...