List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static String nodeToString(Node node) throws TransformerException { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString().replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim(); }
From source file:Main.java
public static String toString(Document doc) throws TransformerException { StringWriter sw = new StringWriter(); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) { try {/*from w w w. j a v a 2 s .c o m*/ 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", "2"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { System.out.println("Error while printing the document."); } }
From source file:Main.java
public static String getDOMString(Document doc) { String s = null;// w ww .ja v a2 s . co m final TransformerFactory tfactory = TransformerFactory.newInstance(); try { final Transformer xform = tfactory.newTransformer(); final Source src = new DOMSource(doc); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); xform.transform(src, result); s = writer.toString(); } catch (final Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
From source file:Main.java
public static void writeXmlFile(Document file, String fileName) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(file); StreamResult result = new StreamResult(new File(fileName)); transformer.transform(source, result); }
From source file:Main.java
public static String xmlToString(Node node) { try {// w ww. j av a2 s .co m Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String returnDocAsString(Document doc) throws TransformerException { StringWriter sw = new StringWriter(); TransformerFactory tfFac = TransformerFactory.newInstance(); // use null trandformation Transformer tf = tfFac.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String transform2String(Node node) throws TransformerException { final TransformerFactory transFactory = TransformerFactory.newInstance(); final Transformer transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); return buffer.toString(); }
From source file:Main.java
public static void write(Element root, String xmlPath) { try {// w ww. j a va 2 s.co m Document doc = root.getOwnerDocument(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("encoding", "utf-8"); FileWriter fw = new FileWriter(xmlPath); transformer.transform(new DOMSource(doc), new StreamResult(fw)); fw.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void write2Xml(Document document) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName))); }