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 a file./*from w w w .j av a 2 s. co 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 { 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(new File(file)); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
public static String generateXml() { DocumentBuilder documentBuilder = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/* ww w . j ava 2 s .co m*/ documentBuilder = docFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); DOMSource docSource = new DOMSource(document); StreamResult result = new StreamResult(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); result.setOutputStream(baos); transformer.transform(docSource, result); } catch (Exception e) { e.printStackTrace(); } return baos.toString(); }
From source file:Main.java
public static String serializeDocument(Document document) throws Exception { // Serialize XML document to String. StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); DOMSource domSource = new DOMSource(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return writer.toString(); }
From source file:Main.java
public static void printDOM(OutputStream ostr, Document doc, String encoding) { try {/*w w w . j a v a 2 s . c o m*/ Transformer tr = TransformerFactory.newInstance().newTransformer(); if (encoding != null) tr.setOutputProperty(OutputKeys.ENCODING, encoding); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); tr.transform(new DOMSource(doc), new StreamResult(ostr)); } catch (TransformerConfigurationException ex) { System.err.println(ex.getMessage()); } catch (TransformerException ex) { System.err.println(ex.getMessage()); } }
From source file:Main.java
/** * ***************************************** * Convert XML document to string//from w w w.j ava 2 s .co m * ******************************************. * * @param xmlDocument the xml document * @return the string * @throws IOException Signals that an I/O exception has occurred. * @throws TransformerException the transformer exception */ public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException { String xmlString = ""; 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"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); return xmlString; }
From source file:Main.java
public static void setCommonOutputProperties(final Transformer transformer, final boolean indentOutput) throws TransformerConfigurationException { transformer.setOutputProperty(OutputKeys.METHOD, XML); transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8); transformer.setOutputProperty(OutputKeys.VERSION, VERSION); if (indentOutput) { transformer.setOutputProperty(OutputKeys.INDENT, YES); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); } else {/* w ww. ja va2s. c o m*/ transformer.setOutputProperty(OutputKeys.INDENT, NO); } }
From source file:Main.java
/** * Creates a new document transformer//from w w w. j a v a2s . com * @return the transformer */ public static Transformer createTransformer() throws TransformerConfigurationException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); return transformer; }
From source file:Main.java
public static void writeToFile(Document doc, String file) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); StreamResult result = new StreamResult(pw); transformer.transform(source, result); }
From source file:Main.java
public static final void prettyPrint(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); System.out.println(out.toString()); }
From source file:Main.java
public static void writeXMLDocument(Document doc, StreamResult out) throws TransformerConfigurationException, TransformerException { DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }