XmlUtil { static void toIndentedString(PlexusConfiguration xml, int indentationSize, int currentDepth, Writer wrt) throws IOException { indent(indentationSize, currentDepth, wrt); boolean hasChildren = xml.getChildCount() > 0; boolean hasContent = xml.getValue() != null && !xml.getValue().isEmpty(); wrt.write('<'); wrt.write(xml.getName()); if (!hasChildren && !hasContent) { wrt.write("/>"); } else { wrt.write('>'); if (hasChildren) { wrt.write('\n'); for (PlexusConfiguration c : xml.getChildren()) { toIndentedString(c, indentationSize, currentDepth + 1, wrt); wrt.append('\n'); } if (!hasContent) { indent(indentationSize, currentDepth, wrt); } } if (hasContent) { escaped(wrt, xml.getValue()); } wrt.write("</"); wrt.write(xml.getName()); wrt.write('>'); } } private  XmlUtil();   }
@Test public void testPrettyPrintingXml() throws Exception { String text = "<a><b><c>asdf</c><d/></b></a>"; XmlPlexusConfiguration xml = new XmlPlexusConfiguration(Xpp3DomBuilder.build(new StringReader(text))); StringWriter wrt = new StringWriter(); XmlUtil.toIndentedString(xml, 2, 0, wrt); String pretty = wrt.toString(); assertEquals("<a>\n <b>\n <c>asdf</c>\n <d/>\n </b>\n</a>", pretty); }