4 Replies Latest reply on Jul 10, 2008 9:35 AM by Emanuele Radaelli

    Write an XML file from a .jsx

    Emanuele Radaelli Level 1
      Hi,
      a basic problem...
      I made an XML var within the script and need to save it as content of an XML file:

      try
      {
           var user = "Bobby";
           var XMLdata = new XML("");
           var XMLfile = new XML("" + user + "");
           XMLdata.appendChild(XMLfile);
           
           var docu = new File(XMLdata);
           docu.write(ExportFormat.XML, File("/Users/Bobby/Desktop/prova.xml"), true);
           
      }

      catch (error)
      {
           $.write(error);
      }

      What's wrong with this script?
        • 1. Re: Write an XML file from a .jsx
          Level 1
          This seems to be the most 'proper' way of writing/reading an XML object to/from
          a File. Setting the lineFeed to "unix" is optional, but it helps me retain my
          sanity :)

          -X

          function writeXMLFile(file, xml) {
          if (!(xml instanceof XML)) {
          throw "Bad XML parameter";
          }
          file.encoding = "UTF8";
          file.open("w", "TEXT", "????");
          // unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
          file.write("\uFEFF");
          file.lineFeed = "unix";
          file.write(xml.toXMLString());
          file.close();
          };

          function readXMLFile(file) {
          if (!file.exists) {
          throw "Cannot find file: " + deodeURI(file.absoluteURI);
          }
          file.encoding = "UTF8";
          file.lineFeed = "unix";
          file.open("r", "TEXT", "????");
          var str = file.read();
          file.close();

          return new XML(str);
          };
          • 2. Re: Write an XML file from a .jsx
            Emanuele Radaelli Level 1
            Thenks xbytor... I was really faaaaaaaaaaaar from this!
            • 3. Re: Write an XML file from a .jsx
              Level 1
              Hi cinematomico,

              xbytor's example is good (thanks, x!)--depending on what you need to do, you might even be able to just write the result of XML.toString() to a text file.

              But I wanted to point out the biggest problem with your original approach, because it might save you time in the future. When you say:

              var docu = new File(XMLdata);
              docu.write(ExportFormat.XML, File("/Users/Bobby/Desktop/prova.xml"), true);

              ...you're really doing several things wrong. First, unless XMLdata is a file path, new File(XMLData) is not going to do anything. But the next line shows that you're confusing InDesign's file export method with ExtendScript's File.write() method. The File object--and the XML object, for that matter, belong to ExtendScript, not InDesign.

              The exportFile method on an InDesign document (which would use the ExportFormat.xml enumeration--note capitalization)would not export an ExtendScript XML object, but could export XMLElements from the structure of an InDesign document.

              Here's the main point: when you see something in the CoreJavaScriptClasses or ScriptUIClasses object models in the ExtendScript Toolkit's object model viewer, they belong to ExtendScript/JavaScript. When you see something in the Adobe InDesign CS3 Object Model, it belong to InDesign. There are various ways to mix these objects in a script, but you can't take a method or property from one and use it as a method or property of the other.

              I know that this can get confusing--many of the terms are the same or similar between the different libraries. Just keep on asking questions, and we'll try to help.

              Thanks,

              Ole
              • 4. Re: Write an XML file from a .jsx
                Emanuele Radaelli Level 1
                Thanks a lot Ole, comments like yours help me to take the right direction when scripting, and now, as a noob, the best direction is the one which is certainly not wrong!
                I understand the difference between ExtendScript/JavaScript and the Adobe InDesign CS3 Object Model but sometimes scripting practice is a world apart... and sometimes official documentation is not enough!

                Thanks again all you guys, this forum is really a great playground...