8 Replies Latest reply on Mar 21, 2010 7:40 PM by [Jongware]

    Import XML... vs .importXML [JS CS4]

    pbear* Level 1

      I'm looking to programmatically import an XML file, but the results are not what I expected.

       

      For example, take this file, sample.xml

      <persons>
         <person pid="0">
           <name>John Q. Public</name>
           <age>54</age>
           <date string="birthday">
             <day>14</day>
             <month>May</month>
             <year>1956</year>
           </date>
         </person>
         <person pid="1">
           <name>Sally Smith</name>
          ...

       

      If I run this code, importSample.jsx

      // a new document was already programmatically created
      var myDocument = app.documents.item(0);
      myDocument.xmlElements[0].importXML(File(Folder.desktop + "/sample.xml"));

       

      I get this:

       

      But if I use File > Import XML... I get this, which is what I want:

       

      The examples I'm finding for scripting XML import are all the same – most results for scripting turn up Adobe's own PDF guides. The XML rules examples in the InDesign CS4 Scripting Guide PDF (Javascript) uses similar XML in its example, but requires it to be imported already. What am I missing here?

        • 1. Re: Import XML... vs .importXML [JS CS4]
          [Jongware] Most Valuable Participant

          I'm sorry to say this, but ... running that javascript on your sample XML results in this:

           

          xmlimport.png

           

          -- which looks suspiciously similar to your "correct" version!

           

          Perhaps a few basic checks are in order:

           

          * is your sample.xml saved as plain text? (I suppose it is, because ID itself can read it. Check anyway, will you?)

          * are you sure the file opened in your script is the one you intended? I put the XML file in the Scripts folder, and used this for path:

             myDocument.xmlElements[0].importXML(File(app.activeScript.path + "/sample.xml"));

          * ... can't think of anything else ...
          (Ed.) My version is not showing any XML contents, but that's because it's a preference which accidentally was "off" on my system. It's still the same.
          1 person found this helpful
          • 2. Re: Import XML... vs .importXML [JS CS4]
            [Jongware] Most Valuable Participant

            Another possibility

             

            Running the script doesn't "open" the XML tree in the Structures panel. If you open the Root item, it shows "persons" but not its contained items. However, since I saw the little triangle on the left, I knew there was more to it and opened that too. Perhaps ... your InDesign forgot to draw that "there-is-more" triangle!

             

            Did you know clicking the Open triangle with the Alt button down (on a Mac) also opens all elements within? I just found that accidentally ... (Plenty surprises left, I guess.)

            • 3. Re: Import XML... vs .importXML [JS CS4]
              pbear* Level 1

              First off, thanks for the reply!

               

              Perhaps a few basic checks are in order:

               

              * is your sample.xml saved as plain text? (I suppose it is, because ID itself can read it. Check anyway, will you?)

               

              As sample.xml or sample.txt, as UTF-8 with or without the BOM – no go.

               

              * are you sure the file opened in your script is the one you intended? I put the XML file in the Scripts folder, and used this for path:

              myDocument.xmlElements[0].importXML(File(app.activeScript.path + "/sample.xml"));

               

              If I remove the one I think I'm using on the Desktop it gives me a modal window saying the file can't be found. I tried the above path with a copy of the XML in the same folder as the script with the same results.

               

              * ... can't think of anything else ...
              (Ed.) My version is not showing any XML contents, but that's because it's a preference which accidentally was "off" on my system. It's still the same.

               

              Running the script doesn't "open" the XML tree in the Structures panel. If you open the Root item, it shows "persons" but not its contained items. However, since I saw the little triangle on the left, I knew there was more to it and opened that too. Perhaps ... your InDesign forgot to draw that "there-is-more" triangle!

               

              Did you know clicking the Open triangle with the Alt button down (on a Mac) also opens all elements within? I just found that accidentally ... (Plenty surprises left, I guess.)

               

              I clicked, option-clicked, command-clicked, command-option clicked... where the disclosure triangle next to "persons" should be, and no results. Clicking the same way on "Root" only discloses "persons". (That's a cool trick for showing all items though.)

               

              If I select "persons" and choose "New Element..." from the Structure Panel menu I get a choice of:

              person
              persons
              Root

              I can't add more to the newly created "person" in the same (or any) way. Options at higher levels remain the same.

               

               

               

              Is one of these wrong?

              function setXMLPrefs(){
                 var myDocument = app.documents.item(0);

                 // XML View Preferences
                 var myXMLViewPreferences = myDocument.xmlViewPreferences;
                 myXMLViewPreferences.showAttributes = true;
                 myXMLViewPreferences.showStructure = true;
                 myXMLViewPreferences.showTaggedFrames = true;
                 myXMLViewPreferences.showTagMarkers = true;
                 myXMLViewPreferences.showTextSnippets = true;

                 // XML Import Prefs
                 var myXMLImportPreferences = myDocument.xmlImportPreferences;
                 myXMLImportPreferences.allowTransform = false;
                 myXMLImportPreferences.createLinkToXML = false;
                 myXMLImportPreferences.ignoreUnmatchedIncoming = true;
                 myXMLImportPreferences.ignoreWhitespace = true;
                 myXMLImportPreferences.importCALSTables = true;
                 myXMLImportPreferences.importStyle = XMLImportStyles.mergeImport;
                 myXMLImportPreferences.importTextIntoTables = false;
                 myXMLImportPreferences.importToSelected = false;
                 myXMLImportPreferences.removeUnmatchedExisting = false;
                 myXMLImportPreferences.repeatTextElements = true;
              }

              • 4. Re: Import XML... vs .importXML [JS CS4]
                [Jongware] Most Valuable Participant

                At this point, I'm happy to leave it to XML experts ...

                All I did was copying your XML into a new file and running the two-line script to import it. It seems one of the settings you set in the full script makes it go goo-goo. Perhaps you could remove one line at a time until it works ... (a.k.a. "debugging by exclusion" )

                • 5. Re: Import XML... vs .importXML [JS CS4]
                  pbear* Level 1

                  It seems one of the settings you set in the full script makes it go goo-goo. Perhaps you could remove one line at a time until it works ... (a.k.a. "debugging by exclusion"  )

                   

                  Now we're cookin'...

                   

                  myXMLImportPreferences.ignoreUnmatchedIncoming = false; // if true, no XML for you!

                   

                  The only thing I don't like is this adds an additional layer of xmlElements[0] to an already tedious path. I thought I read something about E4X with InDesign...

                  var monthOfBirth = persons.person.date.month;

                  would be nice. I guess XML for InDesign is geared towards other sensibilities.

                  • 6. Re: Import XML... vs .importXML [JS CS4]
                    [Jongware] Most Valuable Participant

                    Odd.

                     

                    "If true, ignores elements that do not match the existing structure. Note: Valid only when import style is merge." (from the JS help; my emphasis).

                     

                    It must be some oddity, because I understand why you would use this when updating some data (you wouldn't want a host of new elements pop up), but what's with that note?

                     

                    The only thing I don't like is this adds an additional layer of xmlElements[0]

                     

                    You mean, you're getting your data below the initially existing "Root"? If you rename that to "persons" before running the two-line script (the one that runs without problems ), ID will understand what you were intending to do and add the "person" data immediately below it.

                    • 7. Re: Import XML... vs .importXML [JS CS4]
                      pbear* Level 1

                      Your posted example also has Root > persons > ...

                       

                      I'll give renaming "Root" a try as "persons" from the import is really the root of the file.

                       

                      Thanks for the help!

                      • 8. Re: Import XML... vs .importXML [JS CS4]
                        [Jongware] Most Valuable Participant

                        Your posted example also has Root > persons > ...

                         

                        Yes, I was just wondering what would happen if ... Turns out I was right!