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 String formatXmlAsStringNoWhitespace(Document document) { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); try {/* w ww.j a va 2 s.co m*/ Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException ex) { throw new RuntimeException("Error formatting xml as no-whitespace string", ex); } return writer.toString(); }
From source file:Main.java
/** * Transform xml Document to String// ww w.j a v a 2 s . c om * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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 Transformer newTransformer(boolean indented) { String indentFlag = (indented) ? "yes" : "no"; try {/*from ww w .j a va 2 s .co m*/ TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer; transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, indentFlag); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); return transformer; } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * a debug method//from w ww . j av a2 s . co m * * @param node A node to be dumped to a string * @param omitDeclaration A boolean whether to omit the XML declaration * @return A string representation of the node. * @throws Exception If anything goes wrong. Error handling omitted. */ public static String dumpNode(Node node, boolean omitDeclaration) throws Exception { Transformer xformer = TransformerFactory.newInstance().newTransformer(); if (omitDeclaration) { xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Source source = new DOMSource(node); xformer.transform(source, result); return sw.toString(); }
From source file:Main.java
public static void printDocument(final Document doc) { try {/* ww w. j av a 2 s . c o m*/ final TransformerFactory tf = TransformerFactory.newInstance(); Transformer 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8"))); } catch (final TransformerConfigurationException e) { e.printStackTrace(); } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } catch (final TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Get standard xml string of node//w ww. j ava 2 s .co m * * @param node * @return */ public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String XMLDocumentToString(Document _doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans;/* w w w . j a v a 2 s . co m*/ String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; try { trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(_doc); trans.transform(source, result); xmlString += sw.toString(); } catch (TransformerConfigurationException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } catch (TransformerException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } return xmlString; }
From source file:Main.java
public static void dumpElement(Logger log, Element elem) { try {//from w w w. j a v a 2 s.com TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(elem), new StreamResult(buffer)); log.debug(buffer.toString()); } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) { log.error("Failed to dump element", ex); } }
From source file:Main.java
public static String Document2String(Document doc) throws IOException, TransformerException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(baos, "UTF-8"))); return baos.toString("UTF-8"); }
From source file:Main.java
public static String prettyPrint(File file) { Transformer tf;// w w w.j a v a 2s. c om try { tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult stringResult = new StreamResult(new StringWriter()); tf.transform(new DOMSource(convertFileToDom(file)), stringResult); return stringResult.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }