4 Replies Latest reply on Apr 28, 2009 12:46 AM by IanDBrown

    CS3 javascript XML and attribute adding.

    IanDBrown Level 2

      I've been trying to get something working in CS3 javascript XML for some time and it is getting really frustrating.

       

      Say I have:

      var someXML = new XML("<anElement/>");

       

      someXML.@xxx = "something";
      $.writeln(someXML.toXMLString());

       

      gives the output I'd expect (<anElement xxx="something"/>);

       

      But I need the name of the attribute to be dynamic.  As far as I can see I should be able to either:

       

      someXML.getAttribute("xxx") = "something";

      or

      someXML.@["xxx"] = "something";

       

      both these cases give an error of 'Cannot set property xxx'.  I'm using CS3 version 5.0.4 (the debug build because most of the time I'm working directly with the API - not through scripting).

       

      I looking for ideas to try...

      Ian

        • 1. Re: CS3 javascript XML and attribute adding.
          Olav Martin Kvern Level 3

          Hi Ian,

           

          re: "I'm using CS3 version 5.0.4 (the debug build because most of the time I'm working directly with the API - not through scripting)."

           

          I don't quite understand this...?

           

          The XML objects/methods you're using are part of ExtendScript (and are documented in the JavaScript Tools Guide), not part of the InDesign API. This means you won't be able to get to them via any other route. The InDesign XML objects (XMLElement, XMLAttribute, etc.) are part of the InDesign API.

           

          Help me understand: are you trying to create an XML element in ExtendScript? Or are you trying to create an XML element in InDesign?

           

          Thanks,

           

          Ole

          • 2. Re: CS3 javascript XML and attribute adding.
            IanDBrown Level 2

            Hi Ole, thanks for the reply.

             

            I am aiming to manipulate XML using standard javascript functions - nothing directly to do with indesign, though other parts of the script does interact with it.  Basically the scripts allow a user to set some configuration options and that is in an XML string format (if it was the indesign XML Hierarchy XMLElements, etc I'm pretty sure I could get things to work - worst case I could add to the scripting support in a plugin and make it work...).

             

            I had an idea (driving home after a frustrating day), Since the XML I'm processing is small, I can move forward by crudely making a string from the XML object, inserting the attribute into the string and then re-new'ing the XML object.  With large comments about why - unless someone can come up with a reason/fix for the javascript-XML not working (I suppose it may just be the number of InDesign/Extendscript versions I have installed - CS2/CS3/CS4, indesign and server).

             

            Ian

            • 3. Re: CS3 javascript XML and attribute adding.
              Olav Martin Kvern Level 3

              Hi Ian,

               

              Well, there's always eval():

               

              var someXML = new XML("");
              someXML.@xxx = "something";
              $.writeln(someXML.toXMLString());
              var myString = "yyy";
              eval("someXML.@" + myString + " = \"something\"");
              $.writeln(someXML.toXMLString());
              

               

              It seems to me that there's probably a better way, but I thought that this workaround might help you out in the short term.

               

              Where did you get "getAttribute"? I can't find that anywhere in the XML object reference. There's XML.attributes(string), which is supposed to return the attributes that match the string, but it seems to return the contents of all of the attributes in the element.

               

              Thanks,

               

              Ole

              • 4. Re: CS3 javascript XML and attribute adding.
                IanDBrown Level 2

                Hi Ole,

                 

                Thanks a lot.  Using eval is a good workaround - much better than to/from string, and is working

                 

                Sorry getAttribute was a mistake - it should have been someXML.attribute("xxx") = "something" (which also didn't work for me but should have).

                 

                Thanks again

                Ian