What is the shortest way to pretty print a org.w3c.dom.Document to stdout?

JavaXmlDomW3c

Java Problem Overview


What is the easiest way to pretty print (a.k.a. formatted) a org.w3c.dom.Document to stdout?

Java Solutions


Solution 1 - Java

Call printDocument(doc, System.out), where that method looks like this:

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), 
         new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

(The indent-amount is optional, and might not work with your particular configuration)

Solution 2 - Java

How about:

OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(doc);

Solution 3 - Java

Try jcabi-xml with one liner:

String xml = new XMLDocument(document).toString();

This is the dependency you need:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-xml</artifactId>
  <version>0.14</version>
</dependency>

Solution 4 - Java

private void printNode(Node rootNode, String spacer) {
	System.out.println(spacer + rootNode.getNodeName() + " -> " + rootNode.getNodeValue());
	NodeList nl = rootNode.getChildNodes();
	for (int i = 0; i < nl.getLength(); i++)
		printNode(nl.item(i), spacer + "   ");
}

Solution 5 - Java

This will return a nicely formated output by using recursive descent/ascent.

private static boolean skipNL;
private static String printXML(Node rootNode) {
	String tab = "";
	skipNL = false;
	return(printXML(rootNode, tab));
}
private static String printXML(Node rootNode, String tab) {
	String print = "";
	if(rootNode.getNodeType()==Node.ELEMENT_NODE) {
		print += "\n"+tab+"<"+rootNode.getNodeName()+">";
	}
	NodeList nl = rootNode.getChildNodes();
	if(nl.getLength()>0) {
		for (int i = 0; i < nl.getLength(); i++) {
			print += printXML(nl.item(i), tab+"  ");	// \t
		}
	} else {
		if(rootNode.getNodeValue()!=null) {
			print = rootNode.getNodeValue();
		}
		skipNL = true;
	}
	if(rootNode.getNodeType()==Node.ELEMENT_NODE) {
		if(!skipNL) {
			print += "\n"+tab;
		}
		skipNL = false;
		print += "</"+rootNode.getNodeName()+">";
	}
	return(print);
}

Solution 6 - Java

if you use dom4j, it would be dom4JDOM.asString()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflybywireView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaDennisView Answer on Stackoverflow
Solution 3 - Javayegor256View Answer on Stackoverflow
Solution 4 - JavahannesView Answer on Stackoverflow
Solution 5 - JavaMarkView Answer on Stackoverflow
Solution 6 - JavaRockoderView Answer on Stackoverflow