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 String documentToString(Node document) { try {/*from w w w.j a v a 2 s. com*/ 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString().replace("\r\n", "\n"); } catch (TransformerException e) { throw new IllegalAccessError("Couldn't transform document to string"); } }
From source file:Main.java
public static void saveXMLDocumentAsFile(Document doc, File file) { try {/*w ww . j a v a 2s . c om*/ // Indent & Doctype declaration Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // Prepares to write StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); // Output to file result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { System.out.println("Error on saving XML file."); e.printStackTrace(); } }
From source file:Main.java
public static String formatDocument2(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return new String(out.toByteArray()); }
From source file:Main.java
public static String transferXmlToString(Document doc) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//from ww w . ja v a 2 s. c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties p = t.getOutputProperties(); p.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(p); t.transform(new DOMSource(doc), new StreamResult(bos)); } catch (NullPointerException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } String xmlStr = ""; try { xmlStr = bos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return xmlStr; }
From source file:Main.java
public static void printDocument(Node doc, OutputStream out) { try {//w w w.j a va 2s . co m 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", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void serialize(Source source, Writer writer) throws Exception { serialize(source, writer, ImmutableMap.of(OutputKeys.ENCODING, "UTF-8", OutputKeys.INDENT, "yes")); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) { try {/*ww w .j ava 2 s. c o m*/ 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(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { System.out.println("Error while printing the document."); } }
From source file:Main.java
private static void writeDo(Document doc, Writer w, String encoding) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, result);/*from w w w .j a va 2s . co m*/ }
From source file:Main.java
public static String printDocument(Node doc) throws IOException, 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"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String printDocument(Node doc) throws 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"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }