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
/** * Returns a String representation of the DOM hierarchy rooted at the argument Node. * @param node The root Node of the DOM hierarchy to translate. * @return A String representation of the DOM hierarchy rooted at the * argument Node, or null if the operation fails. *//*from w w w .j ava 2 s.c o m*/ public static String toXMLString(Node node, boolean header) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); if (!header) transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } // try - catch return null; }
From source file:Main.java
/** * Write DOM to a file// www.j a v a 2 s .c om * * @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 String transformXmlToString(Document doc, String encoding) throws ParserConfigurationException, TransformerException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.newDocument(); Element e = doc.getDocumentElement(); Node n = document.importNode(e, true); document.appendChild(n);// w w w. ja va 2 s. c o m // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(document); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Serialize XML Document to string using Transformer * // www. java2s . c o m * @param doc XML Document * @return String representation of the the Document * @throws IOException */ public static String serializeToString(Node node, String encoding) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } writer.flush(); return writer.toString(); }
From source file:Main.java
public static void saveXml(Document dom, File file, boolean omitXmlDeclaration) throws IOException { try {/*from ww w. ja v a 2s . com*/ 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
/** * Transform node.// w w w.j a v a 2 s . c o m * * @param node * the node * @param encoding * the encoding * @param removeXmlHeader * the remove xml header * @param result * the result * @throws Exception * the exception */ private static void transformNode(Node node, String encoding, boolean removeXmlHeader, StreamResult result) throws Exception { TransformerFactory transformerFactory = null; Transformer transformer = null; DOMSource source = null; try { transformerFactory = TransformerFactory.newInstance(); if (transformerFactory == null) { throw new Exception("TransformerFactory error"); } transformer = transformerFactory.newTransformer(); if (transformer == null) { throw new Exception("Transformer error"); } transformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (removeXmlHeader) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } source = new DOMSource(node); transformer.transform(source, result); } catch (TransformerFactoryConfigurationError e) { throw new Exception("TransformerFactoryConfigurationError", e); } catch (TransformerConfigurationException e) { throw new Exception("TransformerConfigurationException", e); } catch (TransformerException e) { throw new Exception("TransformerException", e); } }
From source file:Main.java
/** * Save the XML document to an output stream. * /*from w w w . j ava 2 s . c om*/ * @param doc * The XML document to save. * @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 { final 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 final Result result = new StreamResult(outStream); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
/** * Transform this {@link Node} into a {@link String} representation. * * @param xml// w w w.j av a2s. co m * the xml Node * @return a String representation of the given xml Node */ public static String toString(Node xml) { short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); StringWriter buffer = new StringWriter(); try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(xml), new StreamResult(buffer)); } catch (IllegalArgumentException | TransformerException e) { //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'."); return ""; } return buffer.toString(); }
From source file:Main.java
/** * returns an XML string./*w ww . j ava2s . c o m*/ * * @param pNode Node XML Document node * @return String XML string */ public static String getXML(Node pNode) { String retString = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.transform(new DOMSource(pNode), new StreamResult(out)); retString = out.toString(); out.close(); } catch (Exception ex) { } return retString; }
From source file:Main.java
public void print(OutputStream out) 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.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(this.doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }