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
/** * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8). * <p>//from w ww. j av a2 s . co m * Contains workaround for various JVM bugs. * * @param document * The DOM * @param file * The target XML file * @throws TransformerFactoryConfigurationError * In case of an XML transformation factory configuration error * @throws TransformerException * In case of an XML transformation error * @throws IOException * In case of an I/O error */ public static void saveHumanReadable(Document document, File file) throws TransformerFactoryConfigurationError, TransformerException, IOException { // Various indentation and UTF8 encoding bugs are worked around here TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", new Integer(4)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8"); transformer.transform(new DOMSource(document), new StreamResult(writer)); writer.close(); }
From source file:Main.java
/** * Executes a transformation./*from w ww . j ava 2 s . com*/ * <br>The output encoding is set to UTF-8 * @param source the transformation source * @param result the transformation result * @param indent if true, the output indent key is set to "yes" * @throws TransformerException if an exception occurs */ public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (indent) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } transformer.transform(source, result); }
From source file:Main.java
/** * convert node object into String object. * @param node//from w w w .j a v a2 s.c o m * @param omitXmlDecl * @return */ public static String dom2String(Node node, boolean omitXmlDecl) { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); if (omitXmlDecl) transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException(e); } return stringWriter.getBuffer().toString(); }
From source file:Main.java
static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null;/*www . j a v a2 s . c om*/ try { t = tf.newTransformer(); t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.ENCODING, encoding); } catch (TransformerConfigurationException tce) { assert (false); } DOMSource doms = new DOMSource(doc); StreamResult sr = new StreamResult(os); try { t.transform(doms, sr); } catch (TransformerException te) { IOException ioe = new IOException(); ioe.initCause(te); throw ioe; } }
From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java
public static String xmlToString(Document doc) { StringWriter sw = new StringWriter(); try {//from w w w . ja va 2 s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING); transformer.transform(new DOMSource(doc), new StreamResult(sw)); } catch (TransformerException e) { throw new IllegalArgumentException(e); } return sw.toString(); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out, boolean prettyPrint) 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.ENCODING, "UTF-8"); if (prettyPrint) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); }// w w w . ja v a2s . c o m transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static void toString(Document doc, String encoding, Writer w, boolean indent) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }// w w w.ja v a2 s .co m xformer.transform(source, result); }
From source file:Main.java
public static byte[] serializeToByteArray(Document doc) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {// w w w . j ava 2 s.co m 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"); String encoding = doc.getInputEncoding(); if (encoding == null) encoding = "UTF-8"; transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } return out.toByteArray(); }
From source file:Main.java
public static String toPrettyString(String xml) { int indent = 4; try {//www . j a va 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); } // Setup pretty print options 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, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // Return pretty print xml string StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { System.out.println(xml); throw new RuntimeException( "Problems prettyfing xml content [" + Splitter.fixedLength(100).split(xml) + "]", e); } }
From source file:com.sdl.odata.renderer.util.PrettyPrinter.java
/** * Pretty-print a given XML.//w w w . j a v a 2 s .c om * * @param xml The not-formatted XML. * @return The pretty-printed XML. */ public static String prettyPrintXml(String xml) throws TransformerException, IOException { Source xmlInput = new StreamSource(new StringReader(xml)); try (StringWriter stringWriter = new StringWriter()) { StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); transformer.setOutputProperty(OutputKeys.VERSION, "1.0"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } }