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
/** * Method to transform an XML document into a pretty-formatted string. * @param xml//from w w w . j ava2 s .com * @return * @throws Exception */ public static final String xmlToString(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)); return out.toString(); }
From source file:Main.java
/** * Parse file to xml type// ww w . ja v a 2s. com * * @param transformer * @param source * @param file * @throws FileNotFoundException * @throws TransformerException */ public static void parseFileToXML(Transformer transformer, DOMSource source, File file) throws FileNotFoundException, TransformerException { transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(file); StreamResult streamResult = new StreamResult(pw); transformer.transform(source, streamResult); }
From source file:Main.java
public static String toStringFromDoc(Element elem) { String result = null;//from ww w . j av a 2s . co m StringWriter strWtr = new StringWriter(); StreamResult strResult = new StreamResult(strWtr); TransformerFactory tfac = TransformerFactory.newInstance(); try { javax.xml.transform.Transformer t = tfac.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, // text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(elem), strResult); } catch (Exception e) { System.err.println("XML.toString(Document): " + e); } result = strResult.getWriter().toString(); try { strWtr.close(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static void writeXml(OutputStream os, Node node, String encoding) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(); source.setNode(node);//from w w w . ja va 2 s . c o m StreamResult result = new StreamResult(); result.setOutputStream(os); transformer.transform(source, result); }
From source file:Main.java
public static void writeXml(Document document, File outxml) { try {//from w w w.j a va2 s . co m TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(outxml); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String xmlToString(Document xml) throws TransformerConfigurationException, TransformerException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Transformer transformer = getTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(xml), new StreamResult(bos)); return bos.toString(); }
From source file:Main.java
public static String getDocumentAsXml(Document doc) { StringWriter sw = new java.io.StringWriter(); try {// w w w. jav a 2 s. c om DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static synchronized boolean update(String sourceXML, Document doc) { try {// ww w. java 2s .c om Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, ENCODING); tf.setOutputProperty(OutputKeys.INDENT, INDENT); Writer out = new FileWriter(new File(sourceXML)); tf.transform(new DOMSource(doc), new StreamResult(out)); return true; } catch (TransformerException | IOException e) { e.printStackTrace(); } return false; }
From source file:Main.java
public static void SaveDom(Document dom, String filepath) { try {//from ww w .ja v a2 s . c om TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(dom); StreamResult streamResult = new StreamResult(new FileOutputStream(filepath)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.transform(domSource, streamResult); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void saveXml(Document doc, String filename) { try {// w ww . j a va2s. c om TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }