List of usage examples for javax.xml.transform OutputKeys OMIT_XML_DECLARATION
String OMIT_XML_DECLARATION
To view the source code for javax.xml.transform OutputKeys OMIT_XML_DECLARATION.
Click Source Link
From source file:Main.java
public static void nodeToWriter(Node node, Writer writer) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
public static File saveToFile(File file, Document document) throws TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file Result result = new StreamResult(file.toURI().getPath()); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result);//w w w . j a va2 s. c o m return file; }
From source file:Main.java
public static String getXMLStringFromDocument(Document doc) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {//w w w .j a v a 2 s. c o m transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException("Error while transforming XML document to String : ", e); } String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
/** * Converts an XML document to a formatted XML string. * /* w w w . java 2 s . c o m*/ * @param doc The document to format. * @return Formatted XML document. */ public static String toString(Document doc) { if (doc == null) { return ""; } try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { return e.toString(); } }
From source file:Main.java
private static Transformer createXmlTransformer() throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
/** * Method to get formatted Xml string from Xml source * * @param payload Source//from w w w .j a v a 2 s. com * @return a formatted Xml string */ public static String getXmlStringFromSource(Source payload) { String result = null; StreamResult strResult = new StreamResult(new StringWriter()); if (payload != null) { try { TransformerFactory factory = TransformerFactory.newInstance(); //factory.setAttribute("indent-number", new Integer(2)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); transformer.transform(payload, strResult); } catch (TransformerException e) { e.printStackTrace(); } result = strResult.getWriter().toString(); } return result; }
From source file:Main.java
public static String nodeToString(Node node) throws Exception { if (node == null) { return null; }//from w w w. j av a 2s .co m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Node to string./*w ww . j a va2 s . co m*/ * * @param node the node * @return the string */ public static String nodeToString(final Node node) { final StringWriter sw = new StringWriter(); try { final Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (final TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:Main.java
/** * Returns the XML string representation of a Node. * @param node Node to convert into XML string. * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException /* w w w. j a v a 2 s. co m*/ */ public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException { StringWriter writer = new StringWriter(); DOMSource domSource = new DOMSource(node); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); serializer.transform(domSource, result); return (writer.toString()); }
From source file:Main.java
public static String getStringFromNode(Node node) throws Exception { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); String xml = writer.toString(); return xml;/*from w w w . j a va2 s.c o m*/ }