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
/** * Write an XML document out to an output stream in UTF-8 * @param document - the document to write. * @param outputStream - the stream to which the XML will be written * @param indent - the indent level.//from ww w. j a va 2 s.c om */ public static void writeXmlDocumentToStream(Document document, OutputStream outputStream, int indent) { try { Transformer transformer = transformerFactory.get().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, UTF8); if (indent > 0) { transformer.setOutputProperty(OutputKeys.INDENT, YES); transformer.setOutputProperty(INDENT_AMOUNT, Integer.toString(indent)); } Result result = new StreamResult(outputStream); Source source = new DOMSource(document); transformer.transform(source, result); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void formatDocument(Document document, Writer out, String encoding) throws TransformerException { document.normalizeDocument();// www .ja va2 s. com TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { idTransform.setOutputProperty(OutputKeys.ENCODING, encoding); } idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source source = new DOMSource(document); Result result = new StreamResult(out); idTransform.transform(source, result); }
From source file:Main.java
public static void serialize(Node n, StreamResult sr) throws IOException, TransformerException { TransformerHandler hd = newTransformerHandler(); Transformer serializer = hd.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(n); serializer.transform(source, sr);/* w w w. j a va2 s. com*/ }
From source file:Main.java
public static void xmlToStream(Node n, OutputStream os) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
public static String getDocumentAsString(Node node, boolean prettyXml) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (prettyXml) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); }//from w w w. j av a 2s . c om transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(node), new StreamResult(baos)); return baos.toString("UTF-8"); }
From source file:Main.java
public static void nodeToWriter(Node node, Writer writer) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
public static String toString(Document doc, String encoding, boolean indent) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }/*from w ww.j a v a 2 s. c om*/ xformer.transform(source, result); return sw.getBuffer().toString(); }
From source file:Main.java
public static void xmlToStreamE(Node n, OutputStream os) { try {/*w ww . j av a2 s . c om*/ Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } catch (IllegalArgumentException | TransformerException e) { throw new Error(e); } }
From source file:Main.java
public static void removeHandset(String file, String name) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList devices = doc.getElementsByTagName("devices"); NodeList nodeList = doc.getElementsByTagName("device"); for (int i = 0; i < nodeList.getLength(); i++) { Node deviceNode = nodeList.item(i); if (deviceNode.getTextContent().equals(name)) { devices.item(0).removeChild(deviceNode); }//from www .ja v a2 s.com } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }
From source file:Main.java
/** * Writes XML Document into an xml file. * /* w w w .j av a2s .co m*/ * @param fileName the target file with the full path * @param document the source document * @return boolean true if the file saved * @throws Exception */ public static boolean writeXmlFile(String fileName, Document document) throws Exception { // creating and writing to xml file File file = new File(fileName); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // to prevent XML External Entities attack Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(document), new StreamResult(file)); return true; }