Index: trunk/src/scriptbuilder/structures/XMLBuilder.java
===================================================================
--- trunk/src/scriptbuilder/structures/XMLWriter.java	(revision 201)
+++ trunk/src/scriptbuilder/structures/XMLBuilder.java	(revision 203)
@@ -1,9 +1,25 @@
 package scriptbuilder.structures;
 
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
 /**
- *
+ * Utility methods that build XML elements from content strings.
  * @author Bryan McGuffin
  */
-public class XMLWriter
+public class XMLBuilder
 {
 
@@ -78,3 +94,69 @@
         return output;
     }
+    
+    /**
+     * Prettyprint an XML string.
+     * @param xmlString (works best if it contains no newlines)
+     * @return String that has been nicely formatted with indentation
+     */
+    public static String prettyPrintXML(String xmlString)
+    {
+        System.out.println("prettyprint start: " + xmlString);
+        Document xmlDoc = null;
+        String formattedXML = "";
+        try {
+            xmlDoc = toXmlDocument(xmlString);
+            formattedXML = prettyprintdoc(xmlDoc);
+        } catch (ParserConfigurationException | SAXException | IOException
+                | TransformerException e) {
+            e.printStackTrace();
+        }
+        return formattedXML;
+    }
+    
+ 
+    /**
+     * Prettyprint an XML document.
+     * @param document an "ugly" XML document
+     * @return String nicely formatted XML with indentation
+     * @throws TransformerException 
+     */
+    public static String prettyprintdoc(Document document)
+            throws TransformerException {
+        TransformerFactory transformerFactory = TransformerFactory
+                .newInstance();
+        Transformer transformer = transformerFactory.newTransformer();
+        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        transformer.setOutputProperty(
+                "{http://xml.apache.org/xslt}indent-amount", "2");
+        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+        DOMSource source = new DOMSource(document);
+        StringWriter strWriter = new StringWriter();
+        StreamResult result = new StreamResult(strWriter);
+ 
+        transformer.transform(source, result);
+ 
+        return strWriter.getBuffer().toString();
+ 
+    }
+ 
+    /** Create an XML Document from a string that is in xml format.
+     * 
+     * @param str xml format content
+     * @return Document containing the specified content.
+     * @throws ParserConfigurationException
+     * @throws SAXException
+     * @throws IOException 
+     */
+    public static Document toXmlDocument(String str)
+            throws ParserConfigurationException, SAXException, IOException {
+ 
+        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
+                .newInstance();
+        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+        Document document = docBuilder.parse(new InputSource(new StringReader(
+                str)));
+ 
+        return document;
+    }
 }
