List of usage examples for javax.xml.transform OutputKeys INDENT
String INDENT
To view the source code for javax.xml.transform OutputKeys INDENT.
Click Source Link
From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java
public static String xmlToString(Document doc) { StringWriter sw = new StringWriter(); try {//from ww w .j av a 2 s . co m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING); transformer.transform(new DOMSource(doc), new StreamResult(sw)); } catch (TransformerException e) { throw new IllegalArgumentException(e); } return sw.toString(); }
From source file:edu.unc.lib.dl.util.SOAPUtil.java
private static void print(SOAPMessage msg, StreamResult result) { try {//from w w w. ja v a 2s .co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source sourceContent = msg.getSOAPPart().getContent(); //StreamResult result = new StreamResult(out); transformer.transform(sourceContent, result); } catch (TransformerException e) { throw new RuntimeException(e); } catch (SOAPException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Print the specified XML DOM node to the specified output stream * // w w w . ja v a 2 s . c o m * @param n * the node to print * @param os * the output stream to print to * @throws Exception */ public static void printNode(Node n, OutputStream os) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(n), new StreamResult(os)); }
From source file:Main.java
/** * Save the given document to an output stream. Output is nicely formatted, * with child elements indented by 4 spaces. * //w ww .ja va2 s . c om * @param doc * Document to save * @param outputStream * OutputStream to save to * @throws TransformerException * @throws IOException */ public static void saveDocumentToFormattedStream(Document doc, OutputStream outputStream) throws TransformerException, IOException { Source source = new DOMSource(doc); Result result = new StreamResult(outputStream); Transformer transformer = createTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.transform(source, result); }
From source file:Main.java
/** * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8). * <p>/*from w w w.j a va 2s.co m*/ * Contains workaround for various JVM bugs. * * @param document * The DOM * @param file * The target XML file * @throws TransformerFactoryConfigurationError * In case of an XML transformation factory configuration error * @throws TransformerException * In case of an XML transformation error * @throws IOException * In case of an I/O error */ public static void saveHumanReadable(Document document, File file) throws TransformerFactoryConfigurationError, TransformerException, IOException { // Various indentation and UTF8 encoding bugs are worked around here TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", new Integer(4)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8"); transformer.transform(new DOMSource(document), new StreamResult(writer)); writer.close(); }
From source file:Main.java
/** * /*from w ww . j a v a2 s.co m*/ * <B>Purpose:</B>Converts an XML Document to a String Object * * @param node * @return * @throws TransformerException */ public static String xmlToString(Node node) throws TransformerException { if (node == null) { return ""; } Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); transformer.setOutputProperty("omit-xml-declaration", "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); }
From source file:Main.java
public static void writeTo(Document document, File output) { try {/*from ww w . j a va2 s .c om*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream outputstream = new FileOutputStream(output); StreamResult result = new StreamResult(outputstream); // Manually add xml declaration, to force a newline after it. String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; outputstream.write(xmlDeclaration.getBytes()); // Remove whitespaces outside tags. // Essential to make sure the nodes are properly indented. XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } // Pretty-print options. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(source, result); outputstream.close(); } catch (TransformerException | IOException | XPathExpressionException e) { System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString()); } }
From source file:Main.java
/** * Transform document to string representation. * * @param document// w w w. ja v a 2 s. c o m * XHTML document * @return string representation of document * @throws TransformerException * if it is not possible to create a Transformer instance or to transform document */ public static String toString(final Document document) throws TransformerException { final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); final Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, TRANSFORMER_YES); // set indent for XML transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); // not all JavaSE have the same implementation transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // transformer.setOutputProperty(OutputKeys.METHOD, "html"); // html method transforms <br/> into <br>, which cannot be re-parsed // transformer.setOutputProperty(OutputKeys.METHOD, "xhtml"); // xhtml method does not work for my xalan transformer transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, TRANSFORMER_YES); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); return stringWriter.toString(); }
From source file:Main.java
/** * Save the XML document to an output stream. * * @param doc The XML document to save.// w ww . j a v a 2 s. c om * @param outStream The stream to save the document to. * @param encoding The encoding to save the file as. * * @throws TransformerException If there is an error while saving the XML. */ public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file Result result = new StreamResult(outStream); DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
public static void saveDocument(Document doc, File transform, File file) throws IOException { try {/* ww w . j a va2 s.c om*/ TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(2)); Transformer transformer = tFactory.newTransformer(new StreamSource(transform)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(file.getPath()); DOMSource source = new DOMSource(doc); CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder(); StreamResult result = new StreamResult(new OutputStreamWriter(fos, utf8Encoder)); transformer.transform(source, result); fos.close(); } catch (TransformerException e) { throw new IOException(e); } }