List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
public static String printXMLNode(Node node) { try {// w w w . j a v a 2 s .co m TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 2); Transformer transformer = tf.newTransformer(); String omitDeclaration = node instanceof Document || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes"; transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String output = writer.getBuffer().toString(); output = removeEmptyLines(output); // .replaceAll("\n|\r", ""); return output; } catch (TransformerException te) { throw new RuntimeException(te); } }
From source file:Main.java
private static String docToString(Document doc) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument) { try {/* www .jav a 2s .c om*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult xmlOutput = new StreamResult(new StringWriter()); transformer.transform(new DOMSource(domDocument), xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void writeDomToFile(Document dom, File outFile, Properties outputProperties) { // try { // StringWriter ret = new StringWriter(); // TransformerFactory transFact = TransformerFactory.newInstance(); //// transFact.setAttribute("indent-number", 2); // Transformer transformer = transFact.newTransformer(); // if (outputProperties != null) transformer.setOutputProperties(outputProperties); // DOMSource source = new DOMSource(dom); // StreamResult result = new StreamResult(ret); try {/*from w w w . j ava 2 s.co m*/ TransformerFactory transFact = TransformerFactory.newInstance(); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult( new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding"))); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException( "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e); } }
From source file:Main.java
/** * Converts a DOM Tree to a String.//from w ww .jav a2 s. c om * * @param xml The DOM document to convert. * @param file The file to save to. * * @return true if there were no errors saving to disk. */ public static boolean xmlToFile(Document xml, File file) { try { TransformerFactory f = TransformerFactory.newInstance(); OutputStream writer = new FileOutputStream(file); Transformer t = f.newTransformer(); DOMSource src = new DOMSource(xml); StreamResult result = new StreamResult(writer); t.transform(src, result); writer.close(); return true; } catch (Exception e) { return false; } }
From source file:Main.java
public static String constructXMLString(Document dom) throws IOException { String retXMLStr = null;//from w w w. j av a 2 s . c o m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = factory.newTransformer(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); DOMSource source = new DOMSource(dom); transformer.transform(source, result); writer.close(); retXMLStr = writer.toString(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* // Make a string out of the DOM object System.out.println(dom.toString()); OutputFormat format = new OutputFormat(dom); format.setIndenting(true); ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(byteoutStream, format); serializer.serialize(dom); String retXMLStr = new String(byteoutStream.toByteArray()); byteoutStream.close(); */ return retXMLStr; }
From source file:Main.java
public static Transformer newTransformer(int indent) throws TransformerConfigurationException { TransformerFactory factory = newTransformerFactory(); factory.setAttribute("indent-number", Integer.valueOf(indent)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
public static void prettyPrint(Document document) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//w w w .j ava2 s. co m try { serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(new DOMSource(document), new StreamResult(System.out)); } catch (TransformerException e) { // this is fatal, just dump the stack and throw a runtime exception e.printStackTrace(); throw new RuntimeException(e); } }
From source file:Main.java
public static boolean write(String filename, Document document, boolean addDocType) { try {/* ww w . ja v a 2 s .c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(4)); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); if (addDocType) { transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"); } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))))); return true; } catch (Exception e) { return false; } }
From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java
public static String xmlToString(Document doc) { StringWriter sw = new StringWriter(); try {// w w w . j a v a2s. co m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING); transformer.transform(new DOMSource(doc), new StreamResult(sw)); } catch (TransformerException e) { throw new IllegalArgumentException(e); } return sw.toString(); }