I use this code to transform a Document to a String: public String getXmlStringFromDocument(Document document) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(domSource, streamResult); return stringWriter.toString(); }
The problem is that the XML is in on line like. <a><b>Hello</b><a/> instead of <a> <b>Hello</b> <a/>
Is there a smart and easy way to get the line broken up on several lines?
I found some extra features like: public String getXmlStringFromDocument(Document document) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance();
DOMSource domSource = new DOMSource(document); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(domSource, streamResult); return stringWriter.toString(); } This code now wraps each line but does not indent. This code //transformerFactory.setAttribute("indent-number", new Integer(4)); does not seem to work in 1.4
But this is good enough for me!
Thanks! Fredrik
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.