5 Replies Latest reply on Dec 10, 2009 9:02 AM by Loic.Aigon

    Accessing the currently imported XML file in InDesign with JavascriptIsthere

    jpedroza2kLDS

      Is there a way to access an XML document that has already been imported into InDesign through javascript. I can see how to get a pointer to the document if I import it through my script, but not how to access the XML if it is already linked to the document.

       

      Thanks,

       

      Jared Pedroza

        • 1. Re: Accessing the currently imported XML file in InDesign with JavascriptIsthere
          Loic.Aigon Adobe Community Professional

          Had a look on the DOM and it seems that there is no properties for an XMLElement (considering the element is the root) like filePath.

          Parent is pointing to the document. So obviously you can't get link to the genuine xml file.

          Loic

          • 2. Re: Accessing the currently imported XML file in InDesign with JavascriptIsthere
            jpedroza2kLDS Level 1

            What I am trying to do is replace each instance of a certain XML element from the imported document with an inline anchored text box that contains the contents of the element. So I guess I do not need access to the actual XML document, but rather to the elements within InDesign. I have tried using app.activeDocument.xmlElements.xmlElements.itemByName("<tag>") but I can't access any methods, almost like I am not getting the correct object returned. See code below...

             

            //Load in all the paragraph styles in the current document

            var currentDocument = app.activeDocument;

            //The following are not currently needed.


            //var allParaStyles = currentDocument.allParagraphStyles;

            //alert(allParaStyles[1].name);


            //Create the dialog box to get parameters...

            var settingsDialog = app.dialogs.add({name:"Replace a Tag with an Anchored Box", canCancel:true});


            //Add the fields we need

            with (settingsDialog)

            {

            //Add a dialog column

            with(dialogColumns.add()) {

            //Border panel

            with(borderPanels.add()) {

            with(dialogColumns.add()) {

            staticTexts.add({staticLabel:"Tag:"});

            }

            with(dialogColumns.add()) {

            var myTagSelection = textEditboxes.add({editContents:"Enter tag here..."})

            }

            }

            }

             

            }





            //Display the box


            if (settingsDialog.show() == true)

            {

            var selectedTag;

            //If the user didn't click the cancel button, get the values back from the dialog

            //get the selected XML tag

            selectedTag = myTagSelection.editContents;

            //alert(selectedTag);

            //Find the tag in the document

            var myCurrentXMLElement = currentDocument.xmlElements.itemByName(selectedTag);

             

            }

            • 3. Re: Accessing the currently imported XML file in InDesign with JavascriptIsthere
              Loic.Aigon Adobe Community Professional

              Sorry I understood you were trying to find back the source xml file from the imported XML.

               

              Unless I am wrong, the fact is that you cannot access a xmlElement from anywhere but the parent.

               

              For example :

              <Root>
                  <Tag1>          <Tag2>
                             <Tag3></Tag3>
                        </Tag2>
                   </Tag1>
              <Root>
              

               

              you cant access Tag3 directly and by extension any of its properties and methods.

               

              You have to respect the chain :

               

              doc.xmlElements.itemByName("Root").xmlElements.itemByName("Tag1").xmlElements.itemByName("Tag2").xmlElements.itemByName("Tag3");

               

              then you can access any of the Tag3 properties.

               

              As you request the user the type a tag name, you have to redraw all the chain towards the specified tag.

               

              You can also use libraries that use this kind of stuff. I used this one for a while :

              http://xmljs.sourceforge.net/

               

              Some may shorten the process but I don't know them.

               

              Hope it helps.

               

              Loic

              • 4. Re: Accessing the currently imported XML file in InDesign with JavascriptIsthere
                jpedroza2kLDS Level 1

                Would it be possible to pass an XPath string and have it access the element that way?

                 

                Just wondering at this point.

                 

                J

                • 5. Re: Accessing the currently imported XML file in InDesign with JavascriptIsthere
                  Loic.Aigon Adobe Community Professional

                  XMLJS does that but you still will have to give the hierarchy

                  //Tag1/Tag2/Tag3

                   

                  so the question remains, if you ask the user to specify a tag, how to reconstruct the path ?

                   

                  Loic