List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { if (node == null) return ""; StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void transform(InputStream doc, URL xsltUrl, OutputStream out) throws URISyntaxException, TransformerException { StreamSource source = new StreamSource(doc); StreamSource xslt = new StreamSource(new File(xsltUrl.toURI())); TransformerFactory fac = TransformerFactory.newInstance(); Transformer t = fac.newTransformer(xslt); Result result = new StreamResult(out); t.transform(source, result); }
From source file:Main.java
public static String toString(Element element) { if (element == null) return "null"; Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); Result result = new StreamResult(printWriter); try {// ww w. ja va 2s. c om Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("couldn't write element '" + element.getTagName() + "' to string", e); } printWriter.close(); return stringWriter.toString(); }
From source file:Main.java
public static String GetStringFromDoc(Document d) { /*/*www . ja v a2s .c om*/ DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(d); */ StringWriter output = new StringWriter(); try { //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output)); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output.toString(); }
From source file:Main.java
public static String xmlToString(Node doc) { try {/* w ww . j a va 2 s . c om*/ DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void doSerialize(Document d, File f) throws TransformerException, IOException { Transformer t = tf.newTransformer(); FileOutputStream fos = new FileOutputStream(f); StreamResult sr = new StreamResult(fos); t.transform(new DOMSource(d), sr); fos.close();//from www . ja v a 2 s . c o m }
From source file:Main.java
/** * /* ww w .j a va 2 s .c o m*/ * @param doc * @return */ public static String document2String(Document doc) { try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); // transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
private static void writeDo(Document doc, Writer w, String encoding) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, result); }
From source file:Main.java
/** * Converts an XML {@link org.w3c.dom.Document} to a string. * * @param doc the XML document//from w w w. j av a2s . c om * @return the string representation of the XML document */ public static String documentToString(Document doc) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return ""; }
From source file:Main.java
public static String format(String input, int indent) { try {/*from ww w .j a v a2s . c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }