Index: trunk/src/scriptbuilder/structures/SimulationScript.java
===================================================================
--- trunk/src/scriptbuilder/structures/SimulationScript.java	(revision 203)
+++ trunk/src/scriptbuilder/structures/SimulationScript.java	(revision 206)
@@ -353,13 +353,11 @@
             
         }
-        
-        
-    }
+    }
+
     @Override
     public String toXML()
     {
         ArrayList<TimeSlice> slices = arrangeAllSlices();
-        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
-        output += XMLBuilder.externalDTD();
+        String output = "";
         output += XMLBuilder.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\"");
 
Index: trunk/src/scriptbuilder/structures/XMLBuilder.java
===================================================================
--- trunk/src/scriptbuilder/structures/XMLBuilder.java	(revision 205)
+++ trunk/src/scriptbuilder/structures/XMLBuilder.java	(revision 206)
@@ -84,15 +84,17 @@
 
     /**
-     * Generates the link to external DTD for XML script files.
-     *
-     * @return
+     * @return the xml string with the link to external DTD for XML script files.
      */
     public static String externalDTD()
     {
-        String output = "";
-        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"incident_script.dtd\">\n";
-        return output;
+        return "<!DOCTYPE TMC_SCRIPT SYSTEM \"incident_script.dtd\">\n";
     }
-    
+    /**
+     * @return The xml string that starts the document.
+     */
+    public static String xmlHeader()
+    {
+        return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
+    }
     /**
      * Prettyprint an XML string.
@@ -111,4 +113,8 @@
             e.printStackTrace();
         }
+        // Prepend the header and externalDTD lines.  (This must be done after
+        // prettyprinting, which tries to read the externalDTD if the link is present.)
+        // Fixes #239.
+        formattedXML = xmlHeader() + externalDTD() + formattedXML;
         return formattedXML;
     }
Index: trunk/src/scriptbuilder/gui/application.properties
===================================================================
--- trunk/src/scriptbuilder/gui/application.properties	(revision 201)
+++ trunk/src/scriptbuilder/gui/application.properties	(revision 206)
@@ -1,7 +1,7 @@
-#Wed, 29 Jan 2020 05:08:10 -0800
+#Mon, 03 Feb 2020 16:55:57 -0800
 
-Application.revision=198
+Application.revision=205
 
-Application.buildnumber=54
+Application.buildnumber=55
 
 Incidents.directory=Incidents
