9 Replies Latest reply on Mar 17, 2016 2:14 AM by karthiks94217840

    [JS] Write xml file.

    K159 Level 1


      Hi.


      I want to write xml file using only JavaScript. (Without the xml tag in the document.)


      I tried to below code, but get error.


      please advice for me.


      Thanks.  

       

       

       

      main()
      function main()
      {
          var myDocument = app.activeDocument;
          var myDocumentSize = myDocument.documentPreferences.pageSize;
          var myDocumentWidth = myDocument.documentPreferences.pageWidth;
          var myDocumentHeight = myDocument.documentPreferences.pageHeight;
          
          var tfCount = myDocument.textFrames.count();
      
          var tfContentsArr = new Array(tfCount);
          var tfGeometricArr= new Array(tfCount);
      
          for(var i=0; i<tfCount; i++)
          {
              tfContentsArr[i] = myDocument.textFrames.item(i).contents;
              tfGeometricArr[i] = myDocument.textFrames.item(i).geometricBounds;
          }
          var tagRoot = new XML("<Info>");
          tagRoot[0].DocInfo[0].DocSize[0] = myDocumentSize;
          tagRoot[0].DocInfo[0].DocWidth[0] = myDocumentWidth;
          tagRoot[0].DocInfo[0].DocHeight[0] = myDocumentHeight;
      
          for(var i=0; i<tfCount; i++)
          {
              tagRoot[0].ItemInfo[0].Story[i].contents[0] = tfContentsArr[i];    // error point.   If the <Story>tag two or more.
              tagRoot[0].ItemInfo[0].Story[i].GeometricBounds[0] = tfGeometricArr[i];
          }
      
          alert(tagRoot.toXMLString());
      
          var settingsFile = File(Folder.desktop + "/CompleteDocumentXML.xml");
          settingsFile.open ("w");  
          settingsFile.write ("<?xml version='1.0'?>\r" + tagRoot);
          settingsFile.close ();  
      }
      
      
      
      
      
      
      

       

       

      Output that I want :

       

      <Info>

        <DocInfo>

          <DocSize>Size</DocSize>

          <DocWidth>Width</DocWidth>

          <DocHeight>Height</DocHeight>

        </DocInfo>

        <ItemInfo>

          <Story>

            <contents>contents</contents>

            <GeometricBounds>GeometricBounds</GeometricBounds>

          </Story>

          <Story>

            <contents>contents</contents>

           <GeometricBounds>GeometricBounds</GeometricBounds>

          </Story>

          .....

        </ItemInfo>

      </Info>

        • 1. Re: [JS] Write xml file.
          G. Singelmann Level 3

          Try this:

          main()  function main()  {      var myDocument = app.activeDocument;      var myDocumentSize = myDocument.documentPreferences.pageSize;      var myDocumentWidth = myDocument.documentPreferences.pageWidth;      var myDocumentHeight = myDocument.documentPreferences.pageHeight;            var tfCount = myDocument.textFrames.count();        var tfContentsArr = new Array(tfCount);      var tfGeometricArr= new Array(tfCount);        for(var i=0; itag two or more.          tagRoot.ItemInfo.Story[i].GeometricBounds[0] = tfGeometricArr[i];      }    //    alert(tagRoot.toXMLString());        var settingsFile = File(Folder.desktop + "/CompleteDocumentXML.xml");      settingsFile.open ("w");        settingsFile.write ("\r" + tagRoot);      settingsFile.close ();    }

          • 2. Re: [JS] Write xml file.
            K159 Level 1

            Hi.

             

            tagRoot[0].ItemInfo[0].Story[i].GeometricBounds[0] = tfGeometricArr[i];     

            tagRoot.ItemInfo.Story[i].GeometricBounds[0] = tfGeometricArr[i];  

             

            This line Change, is correct?

             

            I got the same error.

            • 3. Re: [JS] Write xml file.
              G. Singelmann Level 3

              No, sorry, I fail today how to enter code in this forum.

               

              In line 26 you try to write to Story[i].contents before Story[i] even exists.

               

              If you create Story[i] first you should have no problem.

               

              i.e. precede line 26 with:

              tagRoot[0].ItemInfo[0].Story[i] = "aux";

              • 4. Re: [JS] Write xml file.
                K159 Level 1

                Thank you.

                I have finished successfully. 

                • 5. Re: [JS] Write xml file.
                  karthiks94217840 Level 2

                  Hi friend K159,

                   

                  I needed your one important help. Also, the same process going to our office. Please send your javascript coding (successed coding).

                   

                  K159: This very help full of my life.

                   

                  Advance Thanks,

                  • 6. Re: [JS] Write xml file.
                    K159 Level 1

                    Hi

                     


                    main() 
                    function main() 

                        var myDocument = app.activeDocument; 
                        var myDocumentSize = myDocument.documentPreferences.pageSize; 
                        var myDocumentWidth = myDocument.documentPreferences.pageWidth; 
                        var myDocumentHeight = myDocument.documentPreferences.pageHeight; 
                         
                        var tfCount = myDocument.textFrames.count(); 
                     
                        var tfContentsArr = new Array(tfCount); 
                        var tfGeometricArr= new Array(tfCount); 
                     
                        for(var i=0; i<tfCount; i++) 
                        { 
                            tfContentsArr[i] = myDocument.textFrames.item(i).contents; 
                            tfGeometricArr[i] = myDocument.textFrames.item(i).geometricBounds; 
                        }

                        var tagRoot = new XML("<Info>"); 
                        tagRoot.DocInfo.DocSize = myDocumentSize; 
                        tagRoot.DocInfo.DocWidth = myDocumentWidth; 
                        tagRoot.DocInfo.DocHeight = myDocumentHeight; 
                       
                       
                        for(var i=0; i<tfCount; i++) 
                        { 
                            tagRoot.ItemInfo.Story[i] = "";
                            tagRoot.ItemInfo.Story[i].contents = tfContentsArr[i];  
                            tagRoot.ItemInfo.Story[i].GeometricBounds = tfGeometricArr[i]; 
                        } 
                     
                        alert(tagRoot.toXMLString()); 
                     
                        var settingsFile = File(Folder.desktop + "/CompleteDocumentXML.xml"); 
                        settingsFile.open ("w");   
                        settingsFile.write ("<?xml version='1.0' encoding='UTF-8'?>\r" + tagRoot); 
                        settingsFile.close ();   
                    }

                    • 7. Re: [JS] Write xml file.
                      karthiks94217840 Level 2

                      Dear K159,

                       

                      Thanks you for given in your coding k159. Please suggest my doubt in the bellow:

                       

                      1 step: I have run the your script, that showing one dialog box (attached screenshot for your reference)

                       

                      Screen Shot 2016-03-17 at 1.33.27 PM.png

                       

                      2 Step: Creating xml file in my desktop "CompleteDocumentXML.xml" but this xml files is empty (attached screenshot for your reference) . Why was not creating the xml text? please suggest me.

                      Screen Shot 2016-03-17 at 1.32.39 PM.png

                       

                      Thanks,

                      • 8. Re: [JS] Write xml file.
                        K159 Level 1

                        try a short story.

                         

                        And If the encoding does not match, not output.

                        • 9. Re: [JS] Write xml file.
                          karthiks94217840 Level 2

                          Dear K159,

                           

                          OK, i will try

                           

                          Thanks, your valueble comment k159.