4 Replies Latest reply on Nov 11, 2012 1:52 PM by Paul Hough

    Import single element from xml and place into active doc via text box

    Paul Hough

      I am working on an existing .indd file (press sheet) and need to add a text field with content that is stored in a xml file using Javascript in CS5.5. I can make a text box and add content to it but importing a XML and grabbing the value is just beyond my curent skill set. If someone could just push me in the right direction that would be great.

       

      Below is a sample of what the .xml file looks like I am trying to get the bolded string.

       

      ///// Start of XML ////////

       

      <field-list>

          <field Id="spMF_1_5">

              <tag>JbName</tag>

              <type>string</type>

              <required>false</required>

              <value>Test_3498349</value>

          </field>
      </field-list>

       

      //////// End of XML //////

        • 1. Re: Import single element from xml and place into active doc via text box
          Jump_Over Level 5

          Hi,

           

          In general, to import myFile, which is .xml to InDesign use:

           

          app.activeDocument.importXML(myFile);

           

          Regarding to your example,  desired value will be given by:


          app.activeDocument.xmlElements[0].xmlElements[0].xmlElements.itemByName("value").contents;

           

           

          hope it helps...

          1 person found this helpful
          • 2. Re: Import single element from xml and place into active doc via text box
            Paul Hough Level 1

            Thanks Jump_Over!!

             

            Here is the Problemt that I have I can import the XML element ino a textbox using;

             

            var FilterXML

            app.activeDocument.importXML(File.openDialog("Please choose XML File", FilterXML));

            app.activeDocument.pages[0].textFrames[0].parentStory.contents = app.activeDocument.xmlElements[0].xmlElements[0].xmlElements.itemByName("value").contents ;

             

            But when I replace the

            "File.openDialog("Please choose XML File", FilterXML)"

             

            with a file a file path (/HDD/Users/Me/dDescktop/test.xml) the same script fails. I am using a mac if that makes any differance at all.any help would be appreciated greatly!!

             

            Also I have tried using "app.activeDocument.pages[0].textFrames[0].parentStory.contents = FilterXML" to get the file path but this failed aswell

             

            Thanks,

            Paul Hough

            1 person found this helpful
            • 3. Re: Import single element from xml and place into active doc via text box
              [Jongware] Most Valuable Participant

              Paul Hough wrote:

               

              ..  when I replace the

              "File.openDialog("Please choose XML File", FilterXML)"

               

              with a file a file path (/HDD/Users/Me/dDescktop/test.xml) the same script fails.

               

              With what error? (Did you for example  forgot to add quotes around the path?)

               

              Just a random guess: Since CS4 or so, file operations got a bit more picky about what they expect -- a literal "File" object, and nothing as simple as 'a string that is a file path'. You could try this:

               

              app.activeDocument.importXML(File("/HDD/Users/Me/dDescktop/test.xml"));

               

              (I copied the path from your post. Is "dDescktop" really the name of an existing folder?)

              • 4. Re: Import single element from xml and place into active doc via text box
                Paul Hough Level 1

                Jongware,

                 

                Thank you so much for your help!! the "app.activeDocument.importXML(File("/HDD/Users/Me/dDescktop/test.xml")) ;" does exactlly what I had intended.

                 

                The file path in my post was just an example, but you are corect "dDesktop" would not have worked very well .... lol