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:Main.java
/** ask transformer to pretty-print the output * @param transformer will indent output *//* w w w. j a v a 2 s . com*/ public static void setIndentFlag(final Transformer transformer) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // see http://www.w3.org/TR/xslt#output }
From source file:Main.java
/** * Save the XML document to a file./* w w w . ja v a2 s . c om*/ * * @param doc * The XML document to save. * @param file * The file 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(Document doc, String file, String encoding) throws TransformerException { 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(new File(file)); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:Main.java
/** * Creates and configures Transformer object * @return Transformer object or null on failure * @throws TransformerConfigurationException If cannot create *//*from w w w. j a va2s . com*/ public static Transformer createTransformer() throws TransformerConfigurationException { if (s_transformerFactory == null) { synchronized (TransformerFactory.class) { if (s_transformerFactory == null) { s_transformerFactory = TransformerFactory.newInstance(); } } } Transformer transformer = s_transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ return transformer; }
From source file:Main.java
/** * Write DOM to a file/*from www . ja va2s .c o m*/ * * @param doc * DOM document * @param filename * target file name * @param encoding * specified encoding * @param omitXmlDeclaration * flag to indicate if xml declaration statement is included * @throws Exception */ public static void writeXmlFile(Document doc, String filename, String encoding, String omitXmlDeclaration) throws Exception { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file FileOutputStream outputStream = new FileOutputStream(new File(filename)); Result result = new StreamResult(outputStream); // Write the DOM document to the file Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration); transformer.transform(source, result); outputStream.flush(); outputStream.close(); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out, boolean prettyPrint) 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.ENCODING, "UTF-8"); if (prettyPrint) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); }//from w w w . j av a 2 s .c om transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
/** * Executes a transformation.//from w w w .j a v a 2 s . c om * <br>The output encoding is set to UTF-8 * @param source the transformation source * @param result the transformation result * @param indent if true, the output indent key is set to "yes" * @throws TransformerException if an exception occurs */ public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (indent) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } transformer.transform(source, result); }
From source file:Main.java
public static void toString(Document doc, String encoding, Writer w, boolean indent) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }/*w ww .j a va2 s . c o m*/ xformer.transform(source, result); }
From source file:Main.java
public static void saveXml(Document dom, File file, boolean omitXmlDeclaration) throws IOException { try {//from w ww. j a va2s. c om Transformer transformer = getNewTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); transformer.transform(new DOMSource(dom.getDocumentElement()), new StreamResult(file.toURI().getPath())); } catch (Exception e) { throw new IOException("saveXml failed because : " + e.getMessage()); } }
From source file:Main.java
/** * Method to convert document to string. * // w w w.ja va 2 s.co m * @param doc * @return document content as string */ public static String doc2String(Document doc) { try { StringWriter sw = new StringWriter(); 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.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
From source file:Main.java
public static String toPrettyString(String xml) { int indent = 4; try {//www. ja v a2 s . co m // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags 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); } // Setup pretty print options TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // Return pretty print xml string StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { System.out.println(xml); throw new RuntimeException( "Problems prettyfing xml content [" + Splitter.fixedLength(100).split(xml) + "]", e); } }