4 Replies Latest reply on Aug 30, 2010 5:20 AM by mannyk1234

    Iterate through XML tags of file (CS5)




      I'd like to iterate through an XML file, but I am not sure how to do this, since the import is mostly done automatically.


      This is how I load and import the file:

      var f = new File('~/Desktop/test.xml');


      But how can I now iterate through its tags using JavaScript?

        • 1. Re: Iterate through XML tags of file (CS5)
          PankChat Level 1

          You need to look into the tutorial (InDesignCS3_ScriptingGuide_JS)

          available at Adobe website. Look into the XML and XMLRULE topics. That's

          the kick start of understanding the XML in InDesign.


          I do not know about CS5 (yet to work on that) but I am sure nothing have

          changed since CS3 in terms of handling XML in InDesign. Especially for the

          xmlrules (correct me if I am wrong).




          1 person found this helpful
          • 2. Re: Iterate through XML tags of file (CS5)
            mannyk1234 Level 1



            Thank you for the resource!

            However, I was not able to implement any of the example because of an error "__processRuleSet is not a function".


            My code is:

            function mainSetup(){
                 var myDocument = app.documents.add();
                 myDocument.xmlImportPreferences.allowTransform = false;
                 myDocument.xmlImportPreferences.ignoreWhitespace = true;
                 var myFilePath = '~/Desktop/test.xml';
                 var myBounds = myGetBounds(myDocument, myDocument.pages.item(0));
                 myDocument.xmlElements.item(0).placeIntoFrame(myDocument.pages.item(0), myBounds);
                 function myGetBounds(myDocument, myPage){
                     var myWidth = myDocument.documentPreferences.pageWidth;
                     var myHeight = myDocument.documentPreferences.pageHeight;
                     var myX1 = myPage.marginPreferences.left;
                     var myY1 = myPage.marginPreferences.top;
                     var myX2 = myWidth - myPage.marginPreferences.right;
                     var myY2 = myHeight - myPage.marginPreferences.bottom;
                     return [myY1, myX1, myY2, myX2];

            // Test, Add return character after every XML Element
            function mainTest(){
                 if (app.documents.length != 0){
                     var myDocument = app.documents.item(0);
                     //This rule set contains a single rule.
                     var myRuleSet = new Array (new AddReturns);
                         var elements = xmlElements;
                         __processRuleSet(elements.item(0), myRuleSet);
                     alert("No open document");
                     //Adds a return character at the end of every XML element.
                     function AddReturns(){
                         this.name = "AddReturns";
                         //XPath will match on every XML element in the XML structure.
                         this.xpath = "//*";
                         // Define the apply function.
                         this.apply = function(myElement, myRuleProcessor){
                                 //Add a return character after the end of the XML element
                                 //(this means that the return does not become part of the
                                 //XML element data, but becomes text data associated with the
                                 //parent XML element).
                                 insertTextAsContent("\r", XMLElementPosition.afterElement);
                                 //To add the return at the end of the element, use:
                                 //insertTextAsContent("\r", XMLElementPosition.afterElement);
                         return true;// Succeeded
                     } //End of apply function


            Do you have any ideas?


            Kind regards,


            • 3. Re: Iterate through XML tags of file (CS5)
              PankChat Level 1

              I will look into the code later on  and I believe you are including the

              glue code.jsx on top of the script.


              Something like. I cannot see it in snippet you posted.


              #include "glue code.jsx" (if script exist in same folder as glue code)




              #include "/THE COMPLETE PATH OF glue code/glue code.jsx" normally at'


              /Applications/Adobe Indesign CS3 (or whatever version you are

              using)/Scripts/script panel/javascript/xml/

              1 person found this helpful
              • 4. Re: Iterate through XML tags of file (CS5)
                mannyk1234 Level 1

                Thanks, now it is working.

                I am now trying to access the XML Node names and their values...






                Edit: Thanks! Everything is working now.