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
/** * Serialize XML Document to string using Transformer * /* w ww .j a va2s. c o m*/ * @param doc XML Document * @return String representation of the the Document * @throws IOException */ public static String serializeToString(Node node, String encoding) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } writer.flush(); return writer.toString(); }
From source file:Main.java
/** * Pretty prints the document to string using specified charset. * @param document the document/* w ww.j a v a2 s . co m*/ * @param charset the charset * @return printed document in String form * @throws Exception if any errors occur */ public static String prettyPrintXml(Document document, String charset) throws Exception { StringWriter stringWriter = new StringWriter(); StreamResult output = new StreamResult(stringWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, charset); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), output); return output.getWriter().toString().trim(); }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;//from ww w . j a v a2 s . c o m SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:Main.java
public void print(OutputStream out) throws IOException, TransformerException { 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(this.doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:IOUtils.java
public static ContentHandler getSerializer(File file) throws IOException, TransformerException { final FileWriter writer = new FileWriter(file); final TransformerHandler transformerHandler = FACTORY.newTransformerHandler(); final Transformer transformer = transformerHandler.getTransformer(); final Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); format.put(OutputKeys.OMIT_XML_DECLARATION, "no"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); transformer.setOutputProperties(format); transformerHandler.setResult(new StreamResult(writer)); try {// w w w. java 2 s. com if (needsNamespacesAsAttributes(format)) { return new NamespaceAsAttributes(transformerHandler); } } catch (SAXException se) { throw new TransformerException("Unable to detect of namespace support for sax works properly.", se); } return transformerHandler; }
From source file:com.syrup.storage.xml.XmlFactory.java
/** * Convert document to string. Helper method. * /*from www. j a va 2s . c o m*/ * @param document * the document object. * @return String. * @throws java.io.IOException when unable to write the xml * @throws javax.xml.transform.TransformerException when unable to transform the document */ public static String documentToString(Document document) throws IOException, TransformerException { String soapRequest = null; if (document != null) { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(document); transformer.transform(source, result); return result.getWriter().toString(); } return soapRequest; }
From source file:Main.java
/** * Serialize an XML Element into a String. * @param df DocumentFragment to be serialized. * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. *//* ww w.java 2 s . c o m*/ public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) { if (df != null) { try { DOMSource domSource = new DOMSource(df); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; }
From source file:com.jaeksoft.searchlib.util.XmlWriter.java
public XmlWriter(PrintWriter out, String encoding) throws TransformerConfigurationException, SAXException { StreamResult streamResult = new StreamResult(out); SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, encoding); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); transformerHandler.setResult(streamResult); startedElementStack = new Stack<String>(); transformerHandler.startDocument();/* w ww. j a v a 2 s .co m*/ elementAttributes = new AttributesImpl(); Pattern p = Pattern.compile("\\p{Cntrl}"); controlMatcher = p.matcher(""); }
From source file:Main.java
/** * Save the XML document to an output stream. * //from w w w.j av a 2 s .c o 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 { 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); }