0 Replies Latest reply on Apr 11, 2013 12:20 AM by seanbob

    Scripting Selective XML into a Single Text Frame? (& Some Other Stuff...)

    seanbob

      Hi all, I could use some help (using version CS5.5).

       

      I want to import (via Javascript) an XML file similar to this:

       

      <movieRoot>
           <dvds>
                <movie>
                     <title>2001: A Space Odyssey</title>
                     <edition>Special Edition</edition>
                     <features>NOTHING</features>
                     <notes/>
                </movie>
                <movie>
                     <title>Airplane!</title>
                     <edition />
                     <features>Extra Laughs</features>
                     <notes/>
                </movie>
           </dvds> 
      </movieRoot>
      

       

      I've been able to successfully do this, so no problem there.

       

      Then I want to dump the child elements under the XML element "<dvds>" into a single text frame that spans 2 columns per page. That's easy enough too, except:

      1) I want to filter the data so that empty elements or whose contents equal "NOTHING" do not appear.

      2) I want to add static text title above some of the elements that remain (outside the XML proper).

      3) Then I want to format the text (using Paragraph Styles).

       

      Tall order, I know, but I'm keen on figuring it out. I initially created a document using placeholders and used the standard XML Import to populate it with data, which was filtered using scripts to remove extraneous placeholders and static text. It gave me the result I wanted, but as the XML will be regularly updated and since the "filtered" tags result in non-standard XML entries, it means starting over from scratch after every XML update. So I turned to scripting...

       

      For point #1, I was able to filter properly using a recursive IF statement, tested here using message alerts:

      I've included a bunch of variables for (hopefully) easier viewing.

       

      var myXMLRoot = myDocument.xmlElements.item(0);  //movieRoot
      var myXMLParent = myXMLRoot.xmlElements.item(0);  //dvds
      var myXMLElement = myXMLParent.xmlElements.item(0);  //movie
      var currElement = myXMLElement.xmlElements.item(0);  //title
      
      for (i = 0; i < myXMLParent.xmlElements.length; i++)
      {
          myXMLElement = myXMLParent.xmlElements.item(i);
      
          for (j = 0; j < myXMLElement.xmlElements.length; j++)
          {
              currElement = myXMLElement.xmlElements.item(j);
              if (currElement.contents != "" && currElement.contents != "NOTHING") 
              {
                  alert (currElement.markupTag.name + ": " + currElement.contents);
              }
          }
      }
      

       

      It worked great until I tried to get it into an existing text frame. Since I want all the info in one text frame, placexml was out of the question, and markup worked but overwrote XML tags -- such as assigning the entire text frame to <movie/title> when I wanted it assigned to <dvds> with <movie/title> tags nested within it.

       

      If you can help me with anything, this is really where I need pointed in the right direction most. But if you're feeling generous or bored...

       

      Then for points #2 and #3, I want to take what's left ("filtered" XML data) and add additional text and formatting wherever certain tags are found.

      For example, applying the paragraph style "MovieTitles" for any text within <title></title> XML tags; or adding the phrase "Special Features" (with paragraph style "Headers") above any text marked with <features> tags, while the content within those <features> tags applies the paragraph style "NormalText."

       

      I've got some of this worked out in pieces already, but it generally falls apart because I can't get the tag structure correct in the text frame. I can provide more info as requested. Any help would be mighty appreciated!

       

      ----

       

      UPDATE: Surprisingly, I've actually had some good success this evening on solving most of these problems. I'll post my results once I've finished up just in case anyone else can be helped by what I've learned.

       

      One question remains for the moment: I've inserted text within an XML element, but outside of XML tags (e.g., </title>EDITION:<edition>) using "insertTextAsContent":

      [xmlElementHere].insertTextAsContent("EDITION:", XMLElementPosition.beforeElement);  
      

       

      Does anyone know a good way to apply a paragraph style to that text without using a find & replace function later (after placing the XML into a text frame)?