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 elementToString(final Node node) throws Exception { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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"); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final Document document = factory.newDocumentBuilder().newDocument(); final Node importedNode = document.importNode(node, true); document.appendChild(importedNode);//ww w. j a v a 2 s . c o m final ByteArrayOutputStream bos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(document), new StreamResult(bos)); return bos.toString("UTF-8"); }
From source file:Main.java
public static String DocumentToStr(Document doc) throws TransformerException { 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(); }
From source file:Main.java
/** * Write a XML document down to a file.//from ww w . ja v a2s . c om */ public static void writeXmlToFile(Document document, File file) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(document), new StreamResult(file)); }
From source file:Main.java
public static String sourceToString(Source source) throws IOException { try {//from w ww .j a v a2 s . c o m Transformer trans = transFactory.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); Writer writer = new StringWriter(); trans.transform(source, new StreamResult(writer)); writer.flush(); return writer.toString(); } catch (TransformerException ex) { throw new IOException(ex); } }
From source file:Main.java
public static String documentToString(Document doc) throws TransformerException { // Configure the transformer TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // Transform the document DOMSource source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(source, streamResult); return stringWriter.toString(); }
From source file:Main.java
public static String transform2String(Node node) throws TransformerException { final TransformerFactory transFactory = TransformerFactory.newInstance(); final Transformer transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); return buffer.toString(); }
From source file:Main.java
public static String asString(Document doc) { try {/*from ww w. j a va2s . c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (TransformerException e) { throw Throwables.propagate(e); } }
From source file:Main.java
public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;/*ww w. j av a 2 s. c om*/ transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(out)); }
From source file:Main.java
public static String nodeToString(final Node node) { final TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer;//from w w w. j a va 2 s. com try { transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); buffer.append("\n"); return buffer.toString(); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
static public void toXml(Document xmldoc, OutputStream os) throws TransformerException { StreamResult out = new StreamResult(os); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }