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
static public String getPrettyPrint(Document doc) { try {/*from w ww .jav a 2 s .c om*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {//from www . ja v a2 s . c o m Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { throw new RuntimeException(te); } return sw.toString(); }
From source file:com.github.woozoo73.ht.XmlUtils.java
public static String getContent(String path) { try {//from www .ja v a2s . c o m URL url = XmlUtils.class.getClassLoader().getResource(path); File schemaLocation = new File(url.toURI()); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(schemaLocation); StringWriter writer = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(document), new StreamResult(writer)); String content = writer.getBuffer().toString(); return content; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void setDocument(Document document, String fileName) { try {/*from w w w. j a v a 2s .c o m*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(new File(fileName)); transformer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static void writeDocToFile(Document doc, String filename) throws Exception { Source source = new DOMSource(doc); File file = new File(filename); Result result = new StreamResult(file); TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 2); Transformer xformer = tFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result); /*OutputFormat format = new OutputFormat(doc); format.setIndenting(true);//from ww w .j av a2 s. c o m format.setIndent(2); Writer output = new BufferedWriter( new FileWriter(filename) ); XMLSerializer serializer = new XMLSerializer(output, format); serializer.serialize(doc);*/ }
From source file:Main.java
public static void serialize(Source source, Writer writer, Map<String, String> properties) throws Exception { StreamResult sr = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; t = tf.newTransformer();//from ww w . j a v a2s.c o m for (Map.Entry<String, String> me : properties.entrySet()) { t.setOutputProperty(me.getKey(), me.getValue()); } t.transform(source, sr); }
From source file:Main.java
public static InputStream convertMessageToInputStream(Source src) throws IOException, TransformerConfigurationException, TransformerException { final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); transformer.transform(src, result); return new ByteArrayInputStream(baos.toByteArray()); }
From source file:Main.java
public static void document2File(Document document, String encode, File file) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, new StreamResult(file)); }
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
/** * Print a DOM tree to an output stream or if there is an exception while doing so, print the * stack trace.//w w w .j a va 2s. c o m * * @param dom * @param os */ public static void printDom(Node dom, OutputStream os) { Transformer trans; PrintWriter w = new PrintWriter(os); try { TransformerFactory fact = TransformerFactory.newInstance(); trans = fact.newTransformer(); trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os))); } catch (TransformerException e) { w.println("An error ocurred while transforming the given DOM:"); e.printStackTrace(w); } }