4 Replies Latest reply on Dec 10, 2014 9:59 AM by Dirk Becker

    xmlObject - set value of element with a particular attribute

    Ronald63 Level 4

      Hi,

       

      In a xmlObject I try to set the value of xml element with a particular attribute but it doesn't work

       

      //get value of element with a particular attribute
      var myValue = xmlObject.ELEMENTS.ELEMENT.(@category == "myCategory"))// works fine
      //set value of element with a particular attribute
      var myValue = "foo"
      xmlObject.ELEMENTS.ELEMENT.(@category == "myCategory"))= myValue // doesn't work
      

       

      Thanks for your help

      Regards

        • 1. Re: xmlObject - set value of element with a particular attribute
          Dirk Becker Level 4

          It would be helpful if you provided working examples. For example your xmlObject is undefined and your "works fine" has an additional closing bracket.

          Anyway, how about this:

           

          var xmlObject = <xml><ELEMENTS><ELEMENT category="myCategory"/><ELEMENT category="myCategory"/></ELEMENTS></xml>;
          var myValue = xmlObject.ELEMENTS.ELEMENT.(@category == "myCategory");
          $.writeln(myValue.nodeKind());
          $.writeln(myValue.length());
          myValue[0].appendChild("Here");
          $.writeln(xmlObject.toXMLString());
          

           

          Dirk

          • 2. Re: Re: xmlObject - set value of element with a particular attribute
            Ronald63 Level 4

            Hi Dirk,

             

            For example :

            var xmlObject = new XML ( "<rootElement> <elements> <element category='foo1'>value1</element> <element category='foo2'>value2</element> <element category='foo3'>value3</element> </elements></rootElement>");
            myValue = xmlObject.elements.element.(@category == "foo1");
            $.writeln(myValue)// return value1
            // Now I want modify value1 to value99 like this
            xmlObject.elements.element.(@category == "foo1")= "value99"; //doesn't work
            

             

            I hope I was clear in my explanation ;-)


            Regards

            • 3. Re: Re: Re: xmlObject - set value of element with a particular attribute
              Ronald63 Level 4

              Hi,

               

              I found a solution ...

              var xmlObject = new XML ( "<rootElement> <elements> <element category='foo1'>value1</element> <element category='foo2'>value2</element> <element category='foo3'>value3</element> </elements></rootElement>");
              myValue = xmlObject.elements.element.(@category == "foo1");
              $.writeln(myValue)// return value1
              // Now I want modify value1 to value99 like this
              xmlObject.elements.element.(@category == "foo1").setChildren("value99") // works fine
              myValue = xmlObject.elements.element.(@category == "foo1");
              $.writeln(myValue)// return value99
              
              

               

              Thank for your help

              • 4. Re: Re: Re: xmlObject - set value of element with a particular attribute
                Dirk Becker Level 4

                This E4X(?) XML syntax is powerful but you can easily get lost with wrong node kinds. That's why my answer included the $.writeln of the kind - "list" - and used an index for the modification; I found after posting that your assignment would also have worked on the indexed single element. Thanks for sharing your finding with setChildren() - that apparently also works on lists.

                 

                Dirk