1 Reply Latest reply on Oct 9, 2018 4:37 PM by T_Stevens

    Import multiple XML files not working

    T_Stevens Level 1

      I'm working on a quiz booklet where each question is in its own xml file, so I'm trying to import multiple XML files into a single InDesign document. I'm attempting to append each XML inside the Root element, so I can just place the root into a text frame and have all the questions in a single story. My code is below, but it errors out and I can't figure out why.

       

      var myDoc = app.activeDocument;
      
      var myTransform = File.openDialog("Select XSL transform");
      
      var myXMLImportPreferences = myDoc.xmlImportPreferences;  
      myXMLImportPreferences.allowTransform = true;  
      myXMLImportPreferences.createLinkToXML = true;  
      myXMLImportPreferences.ignoreUnmatchedIncoming = false;  
      myXMLImportPreferences.ignoreWhitespace = false;  
      myXMLImportPreferences.importCALSTables = true;  
      myXMLImportPreferences.importStyle = XMLImportStyles.appendImport;  
      myXMLImportPreferences.importTextIntoTables = false;  
      myXMLImportPreferences.importToSelected = true;  
      myXMLImportPreferences.removeUnmatchedExisting = false;  
      myXMLImportPreferences.repeatTextElements = false;
      myXMLImportPreferences.transformFilename = myTransform;
      
      var xmlFolder = Folder.selectDialog("Select the folder with the XML files");
      if(xmlFolder !=null){
          var xmlFiles = GetSubFolders(xmlFolder); //returns array of all xml files in xmlFolder and its subfolders
          }
      
      
      main();
      function main(){ 
          for(var x = 0; x < xmlFiles.length; x++){
              $.writeln(xmlFiles[x].name);
              if(xmlFiles[x] != null){
                  app.select(myDoc.xmlElements.item(0));
                  myDoc.importXML(xmlFiles[x]);
                  }
              }
          }
      
      
      
      
      
      
      

       

      The error occurs on line 30, and all Extendscript does is highlight that line and display "importXML" where the error would normally appear. If I manually import the XML files, everything works. I don't have any issues with the xml or the transform. This code worked once, but only once, and every time since then, I get the same weird "importXML" error.

       

      Any ideas?

        • 1. Re: Import multiple XML files not working
          T_Stevens Level 1

          Here's a fun new development, the code works great if I don't include a transform. If I set myXMLImportPreferences.allowTransform to false, it runs with no errors. When I set it back to true, the script errors after 0-2 xml files import.

           

          The transform I'm using strips out some non-printing elements and strips excess whitespace. Without it, I get a bunch of extra hard returns and tabs and a bit of text.