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
/** * Convert xml to byte array.//from ww w.j av a 2s. c o m * * @param pDocument * the p document * @param encoding * the encoding * @return the byte[] * @throws Exception * the exception */ public static byte[] convertXML2ByteArray(Node pDocument, String encoding) throws Exception { TransformerFactory transformerFactory = null; Transformer transformer = null; DOMSource domSource = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult result = null; try { transformerFactory = TransformerFactory.newInstance(); if (transformerFactory == null) { throw new Exception("TransformerFactory error"); } transformer = transformerFactory.newTransformer(); if (transformer == null) { throw new Exception("Transformer error"); } if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } else { transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } domSource = new DOMSource(pDocument); result = new StreamResult(outputStream); transformer.transform(domSource, result); return outputStream.toByteArray(); } catch (TransformerFactoryConfigurationError e) { throw new Exception("TransformerFactoryConfigurationError", e); } catch (TransformerConfigurationException e) { throw new Exception("TransformerConfigurationException", e); } catch (TransformerException e) { throw new Exception("TransformerException", e); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException ex) { throw new Exception("IO error", ex); } } }
From source file:org.eclipse.swordfish.core.util.xml.XmlUtil.java
private static Transformer getTransformer() { if (transformerFactory == null) { transformerFactory = TransformerFactory.newInstance(); }//w ww . j a v a 2 s . c o m Assert.notNull(transformerFactory); Transformer ret; try { ret = transformerFactory.newTransformer(); } catch (TransformerConfigurationException ex) { throw new RuntimeException(ex); } Assert.notNull(ret); ret.setOutputProperty(OutputKeys.ENCODING, defaultCharset); return ret; }
From source file:Main.java
public static String toString(Node element) throws Exception { if (element == null) { return "null"; }//from w w w. j ava2 s.c o m Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { Result result = new StreamResult(printWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } return stringWriter.toString(); }
From source file:Main.java
/** * Serialize an XML Element into a String. * @param e Element to be serialized./*from w w w . j a va 2s . c o m*/ * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. */ public static final String serialize(Element e, boolean omitXMLDeclaration) { if (e != null) { try { DOMSource domSource = new DOMSource(e); 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:Main.java
/** ask transformer to use UTF-8 * @param transformer will encode output as UTF-8 *//*www .j a v a2 s.co m*/ public static void setUtfEncoding(final Transformer transformer) { transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); }
From source file:Main.java
public static void addApp(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("app"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("aut"); device.setTextContent(name);//from w w w. j a v a 2s. co m for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //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
public static void addHandset(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("devices"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("device"); device.setTextContent(name);//from w w w .j av a2 s. c o m for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //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
/** * Convert the document to an array of bytes. * * @param doc The XML document./*from ww w . j a v a2 s .co m*/ * @param encoding The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, 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"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); try { return writer.getBuffer().toString().getBytes(encoding); } catch (UnsupportedEncodingException e) { throw new TransformerException("Unsupported Encoding", e); } }
From source file:Main.java
public static void xmlToStream(Node n, Writer writer) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(writer);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null;// w w w .j a v a 2 s. co m try { t = tf.newTransformer(); //SF:no dtd 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; } }