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
public static void writeXml(Document document, OutputStream out) { try {/*from www. ja va2s .co m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(out); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out). * * @param _docOrNode {@link Document} or {@link Node} object * @param _outStream {@link OutputStream} to print on * @throws IOException on error//from w w w . j a va 2 s. co m */ public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException { if (_docOrNode == null || _outStream == null) { throw new IOException("Cannot print (on) 'null' object"); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { 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(_docOrNode), new StreamResult(new OutputStreamWriter(_outStream, "UTF-8"))); } catch (UnsupportedEncodingException | TransformerException _ex) { throw new IOException("Could not print Document or Node.", _ex); } }
From source file:Main.java
public static String nodeToString(Node node) throws Exception { if (node == null) { return null; }//from w ww . ja v 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
public static void writeXmlFile(Document doc, File file, boolean indent, String encoding) throws TransformerFactoryConfigurationError, TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file Result result = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }/*from ww w . jav a 2s. c o m*/ xformer.transform(source, result); }
From source file:Main.java
public synchronized static void writeXml(Document document, Writer writer, String encoding) throws TransformerFactoryConfigurationError, TransformerException { if (document != null && writer != null) { Source source = new DOMSource(document); Result result = new StreamResult(writer); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, (encoding == null || encoding.isEmpty()) ? "UTF-8" : encoding); xformer.transform(source, result); }//from w w w. j a v a 2s . c o m }
From source file:Main.java
/** * Create and returns a new Transformer. *//*www . jav a2 s.c om*/ public static Transformer createTransformer() { initFactories(); final Transformer result; try { result = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { return null; } result.setOutputProperty(OutputKeys.METHOD, "xml"); result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); result.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); result.setOutputProperty(OutputKeys.INDENT, "yes"); return result; }
From source file:Main.java
/** * Convert xml to string./*from w w w .j av a 2 s. co m*/ * * @param pDocument * the p document * @param encoding * the encoding * @return the string * @throws Exception * the exception */ public static String convertXML2String(Document pDocument, String encoding) throws Exception { TransformerFactory transformerFactory = null; Transformer transformer = null; DOMSource domSource = null; StringWriter writer = new java.io.StringWriter(); StreamResult result = null; try { transformerFactory = TransformerFactory.newInstance(); if (transformerFactory == null) { throw new Exception("TransformerFactory error"); } transformer = transformerFactory.newTransformer(); if (transformer == null) { throw new Exception("Transformer error"); } if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } else { transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } domSource = new DOMSource(pDocument); result = new StreamResult(writer); transformer.transform(domSource, result); return writer.toString(); } catch (TransformerFactoryConfigurationError e) { throw new Exception("TransformerFactoryConfigurationError", e); } catch (TransformerConfigurationException e) { throw new Exception("TransformerConfigurationException", e); } catch (TransformerException e) { throw new Exception("TransformerException", e); } finally { try { if (writer != null) { writer.close(); } } catch (IOException ex) { throw new Exception("IO error", ex); } } }
From source file:Main.java
public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) { String sOmit = (bOmitDeclaration ? "yes" : "no"); try {/* ww w . j a va 2s . c om*/ TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); if (sEncoding != null) transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit); transformer.transform(new DOMSource(dom), new StreamResult(buffer)); return buffer.toString(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String toStringE(Node element) { try {//from w ww. ja v a 2s . c o m if (element == null) { return "null"; } Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { Result result = new StreamResult(printWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } return stringWriter.toString(); } catch (IllegalArgumentException | TransformerException ex) { throw new Error(ex); } }
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {/*from w ww . ja v a 2s .c o m*/ // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }