2 Replies Latest reply on Dec 26, 2016 11:45 PM by Trevorׅ

    [JS] How to set xml attribute?

    K159 Level 1

      Hi

       

      I am write xml file using javascript.

       

      But I don't know how to set the xml attribute.

       

       

      Below is sample code :

       

      var tagRoot = new XML("<test>");  
      
      tagRoot.FirstElement = "First";
      tagRoot.SecondElement = "Second";
      tagRoot.SecondElement.attribute("???");    // <- I don't know what value to input
          
      alert(tagRoot);  
      

       

       

       

       

       

      Below is what I want :

       

      <test>

           <FirstElement>First</FirstElement>

           <SecondElement SecondAttr = "xxx">Second</SecondElement>

      </test>

        • 1. Re: [JS] How to set xml attribute?
          Trevorׅ Adobe Community Professional

          Hi

           

          Use tagRoot.SecondElement.@SecondAttr = "xxx"

           

          tagRoot.SecondElement.attributes() is a function / method for retrieving the elements  attributes not a property for setting.

          Get it? not set it!

           

          HTH

           

          Trevor

           

          var myRoot = <test>
              <FirstElement>First</FirstElement>
              <SecondElement SecondAttr = "xxx">Second</SecondElement>
          </test>;
          myRoot.FirstElement.@FirstAttr = "1st attr";
          alert(myRoot);
          alert(myRoot.FirstElement.attributes());
          
          • 2. Re: [JS] How to set xml attribute?
            Trevorׅ Adobe Community Professional

            In your style

             

            var tagRoot = new XML("<test>");
            tagRoot.FirstElement = "First"; 
            tagRoot.SecondElement = "Second"; 
            tagRoot.SecondElement.@SecondAtt = "xxx";    // <- I know what value to input 
            alert(tagRoot);