3 Replies Latest reply on Feb 1, 2016 12:18 PM by frameexpert

    XML declaration

    KuddRoww

      When writing an XML object to file is it possible to include the XML declaration?


      I've tried the toXMLString() function and that doesn't seem to work, I get the XML file contents just fine but no declaration.


      I've tried adding it as a string literal like so (the book object is simple an XML object containing all of the book contents):

       

      var xmlFile = File(chapterFolder+app.activeDocument.name.replace(".indd","")+".xml");
      xmlFile.open('w');
      xmlFile.encoding = 'UTF-8';
      xmlFile.write("""<?xml version="1.0" encoding="UTF-8"?>"""+"\r"+book.toXMLString());
      xmlFile.close();
      

       

      Is there a property I'm missing that would allow me to have the declaration included automatically?

       

      Cheers,

       

      Kurtis

        • 1. Re: XML declaration
          frameexpert Level 4

          Hi Kurtis,

           

          Is it an ExtendScript XML object? I am able to do this in one of my scripts and get the declaration:

           

          var settingsXML = <settings><test-element>Element text</test-element></settings>;
          saveSettingsFile (settingsXML);
          
          function saveSettingsFile (settingsXML) {
          
              var settingsFile = File ($.fileName.replace (/\.jsx(?:bin)?$/i , ".xml"));
              settingsFile.open ("w");
              settingsFile.write ("<?xml version='1.0'?>\r" + settingsXML);
              settingsFile.close ();
          
          }
          

           

          This script give me this result:

           

          <?xml version='1.0'?>
          <settings>
            <test-element>Element text</test-element>
          </settings>
          

           

          -Rick

          • 2. Re: XML declaration
            KuddRoww Level 1

            Thanks for testing! That works. I didn't realize that I'm outputting two XML files, one with a file name including date and one without. I was testing the code against the one and checking the other. Thanks for taking the time to test and respond to this.

            • 3. Re: XML declaration
              frameexpert Level 4

              My pleasure. Thanks for marking it Correct!