print pretty indent documentbuilderfactory java xml formatting transformer

java - pretty - Sangrado XML hecho con Transformer



xml format java (2)

Intente establecer la cantidad de sangría, AFAIK el valor predeterminado es 0.

trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")

Intento crear XML a partir de Java y tengo problemas con la sangría. En el siguiente código, puede ver OutputKeys.INDENT establecido en yes ...

//set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); //create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); String xmlString = sw.toString(); //print xml System.out.println(xmlString);

pero parece no tener ningún efecto, la salida es:

<dataset id="1"><br> <path></path><br> <session id="1"><br> <method><br> <timestamp>a timestamp</timestamp><br> <signiture><br> <classPath></classPath><br> <name>methodName</name><br> <declarationType>String</declarationType><br> <parameters><br> <parameter>String</parameter><br> <parameter>int</parameter><br> </parameters><br> </signiture><br> <arguments><br> <argument>SomeValue</argument><br> <argument>AnotherValue</argument><br> </arguments><br> <return>ReturnValue</return><br> </method><br> </session><br> </dataset><br>


Document doc; ..... TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new File("filename.xml"))); transformer.transform(new DOMSource(doc), new StreamResult(System.out));