List of usage examples for javax.xml.transform OutputKeys ENCODING
String ENCODING
To view the source code for javax.xml.transform OutputKeys ENCODING.
Click Source Link
From source file:Main.java
/** * Save the XML document to an output stream. * * @param doc The XML document to save.//from ww w .java 2 s . c o m * @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 { 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(outStream); DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
/** * Creates a {@link TransformerHandler}. * //from www . j av a2 s . c o m * @param commentHeader the comment header * @param rootTag the root tag * @param streamResult stream result */ public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag, StreamResult streamResult, Charset charset) throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { tf.setAttribute("indent-number", new Integer(2)); } catch (Exception e) { // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510 } TransformerHandler transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, charset.name()); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); transformerHandler.setResult(streamResult); transformerHandler.startDocument(); String newline = System.getProperty("line.separator"); if (newline == null) { newline = "\n"; } commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " "); transformerHandler.characters("\n".toCharArray(), 0, 1); transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length); transformerHandler.characters("\n".toCharArray(), 0, 1); if (rootTag.length() > 0) { transformerHandler.startElement("", "", rootTag, null); } return transformerHandler; }
From source file:Main.java
/** * Save the XML document to a file.//w w w. jav 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
/** * Write DOM to a file//from ww w . j ava 2 s. co 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 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);/*from w ww . j av a 2s. com*/ // 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
/** * Transform node./*from w w w. j a v a 2s. com*/ * * @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
/** * Method to convert document to string. * //from w w w. ja va 2 s. c o 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
/** * Save the XML document to an output stream. * //from w ww . j ava2s . co m * @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. jav a 2s . com*/ * 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
/** * Saves a WC3 DOM by writing it to an XML document. * * @param doc The WC3 DOM document object. * @param docPath The full path to the XML document. * @param encoding Encoding scheme to use for the XML document, e.g., * "UTF-8."//from w ww . j a v a2 s . c om * @throws TransformerConfigurationException * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws TransformerException * @throws IOException */ public static void saveDocument(final Document doc, String encoding, String docPath) throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException, TransformerException, IOException { TransformerFactory xf = TransformerFactory.newInstance(); xf.setAttribute("indent-number", 1); //NON-NLS Transformer xformer = xf.newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS xformer.setOutputProperty(OutputKeys.VERSION, "1.0"); File file = new File(docPath); try (FileOutputStream stream = new FileOutputStream(file)) { Result out = new StreamResult(new OutputStreamWriter(stream, encoding)); xformer.transform(new DOMSource(doc), out); stream.flush(); } }