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 serializeXML(Node e, Writer out) throws Exception { DOMSource domSource = new DOMSource(e); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); // turn off <?xml...?> stuff as for documents that were parsed with // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which // it should not, since we always serialize as UTF-8 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); }
From source file:Main.java
/** * Transform xml Document to String// ww w. j av a 2 s. c o m * @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 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 void printDocument(final Document doc) { try {//from w w w . j ava 2s .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
/** * Convert the document to an array of bytes. * * @param doc The XML document./*from w ww . j a v a 2s.com*/ * @param encoding The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, 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"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
/** * Serialize XML from a {@link Document} to an OutputStream. * @param doc//w ww.j a v a 2s .c om * @param out */ public static void serialiseXml(Document doc, Writer out) { DOMSource domSource = new DOMSource(doc.getDocumentElement()); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = null; try { serializer = tf.newTransformer(); } catch (TransformerConfigurationException e) { } serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); try { serializer.transform(domSource, streamResult); } catch (TransformerException e) { } }
From source file:Main.java
/** * Save an XML document into a file/* w ww . j av a2 s. c o m*/ * * @param document * the XML document to save * @param file * the file * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void docToFile(final Document document, final File file) throws TransformerFactoryConfigurationError, TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.transform(new DOMSource(document), new StreamResult(file)); }
From source file:Main.java
/** * Convert the document to an array of bytes. * //from w ww . ja v a 2 s . c om * @param doc * The XML document. * @param encoding * The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException * If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
public static void print(Node node, OutputStream out) throws UnsupportedEncodingException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); 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", "2"); transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
/** * Save the XML document to a file.// w w w . j av a 2s.c o m * * @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 { 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(new File(file)); DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }