11949857191483907492gnome-mime-document.svg.med

Let me start this off with a disclaimer: You may never need this.

Today, I was debugging an integration I needed to work on, and I came across a quick way to print out a Java XML Document object to a string for quick inspection in the log.

In order to do this, I created a “printDocument” function that I used in my script to help me see the request coming in from my scripted web service. Here is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function printDocument(doc){
   var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
   var transformer = tf.newTransformer();
   transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "no");
   transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, "xml");
   transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "yes");
   transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.ENCODING, "UTF-8");
   transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
   var writer = Packages.java.io.StringWriter();
   transformer.transform(new Packages.javax.xml.transform.dom.DOMSource(doc),
   new Packages.javax.xml.transform.stream.StreamResult(writer));
   var output = writer.getBuffer().toString().replaceAll("\n|\r", "");
   gs.log("XML DOCUMENT STRING: \n"+output);
}