List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
private static void wirteXmlFile(Document doc, File file) throws TransformerException { // Write the content into xml file //logger.debug("Changes are done in xml file, Writing to the XML file"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
public static String serializeDocument(Document document) throws Exception { // Serialize XML document to String. StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); DOMSource domSource = new DOMSource(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return writer.toString(); }
From source file:Main.java
public static void serializeXML(Node e, Writer out) throws Exception { DOMSource domSource = new DOMSource(e); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); // turn off <?xml...?> stuff as for documents that were parsed with // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which // it should not, since we always serialize as UTF-8 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); }
From source file:Main.java
public static void saveDoc(Document docToSave, String filePath) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(docToSave); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); }
From source file:Main.java
public static void marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception { TransformerFactory transFac = TransformerFactory.newInstance(); Transformer trans = transFac.newTransformer(); if (indent) { trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); }/*from w ww .j a va 2s . c o m*/ trans.setOutputProperty(OutputKeys.STANDALONE, "no"); trans.transform(new DOMSource(doc), new StreamResult(ostream)); }
From source file:Main.java
public static void marshalToStream(Element elm, OutputStream ostream, boolean indent) throws Exception { TransformerFactory transFac = TransformerFactory.newInstance(); Transformer trans = transFac.newTransformer(); if (indent) { trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); }// ww w. j a va 2 s .c om trans.setOutputProperty(OutputKeys.STANDALONE, "no"); trans.transform(new DOMSource(elm), new StreamResult(ostream)); }
From source file:Main.java
public static void xmlToFile(Document doc, String fileNameToWrite) throws Exception { DOMSource domSource = new DOMSource(doc); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileNameToWrite))); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, streamResult); }
From source file:Main.java
public static String parse(Document document) { try {// ww w . j a va 2 s. co m TransformerFactory dbf = TransformerFactory.newInstance(); Transformer t = dbf.newTransformer(); t.setOutputProperty("encoding", "utf-8"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); t.transform(new DOMSource(document), new StreamResult(bos)); return bos.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String transformToString(Document doc) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); }
From source file:Main.java
public static void printDocument(Node doc, OutputStream out) { try {/* ww w.j a va2 s .c om*/ 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", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { e.printStackTrace(); } }