public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { 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"))); }
public static final Transformer createIndentingTransformer() { Transformer xformer; try { xformer = transformerFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2)); } catch (Exception ex) { throw new IllegalStateException(ex); } xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return xformer; }
private Transformer getTransformer() throws TransformerException { final TransformerFactory xformFactory = TransformerFactory.newInstance(); final Transformer transformer = xformFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); return transformer; }
public static final void writeXml(Document document) { Transformer transformer = createIndentingTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); try { StringResult streamResult = new StringResult(); transformer.transform(new DOMSource(document), streamResult); } catch (Exception ex) { throw new IllegalStateException(ex); } } public static final Transformer createIndentingTransformer() {
/** * Returns a transformer handler that serializes incoming SAX events to * XHTML or HTML (depending the given method) using the given output encoding. * * @param encoding output encoding, or <code>null</code> for the platform default */ private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException { TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler(); Transformer transformer = transformerHandler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, method); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } transformerHandler.setResult(new StreamResult(new PrintStream(out))); return transformerHandler; }
Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); System.out.println(xmlString);
/**** * Converts the document to a well formatted Xml string * * @return pretty xml string */ public static String prettyXml(Node doc) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transform(transformer, doc); }
TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); String str = buffer.toString();
private Transformer getTransformer() throws TransformerException { // Get a TransformerFactory object. TransformerFactory xformFactory = null; try { final Class<?> factoryClass = Class .forName("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); xformFactory = (TransformerFactory) factoryClass.newInstance(); } catch (Exception e) { xformFactory = TransformerFactory.newInstance(); } Log.info("TransformerFactory=" + xformFactory.getClass()); // Get an XSL Transformer object. final Transformer transformer = xformFactory.newTransformer(); Log.info("Transformer=" + transformer.getClass()); // // Sets the standalone property in the first line of // // the output file. transformer.setOutputProperty(OutputKeys.STANDALONE, "no"); // transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "SVG 1.1"); // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); return transformer; }
private void dumpDocToWriter(Document doc, Writer writer) { try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "codefragment"); transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { throw new IllegalStateException(e); } }
@Override public boolean init( StepMetaInterface smi, StepDataInterface sdi ) { meta = (XMLJoinMeta) smi; data = (XMLJoinData) sdi; if ( !super.init( smi, sdi ) ) { return false; } try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); if ( meta.getEncoding() != null ) { transformer.setOutputProperty( OutputKeys.ENCODING, meta.getEncoding() ); } if ( meta.isOmitXMLHeader() ) { transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" ); } transformer.setOutputProperty( OutputKeys.INDENT, "no" ); setTransformer( transformer ); // See if a main step is supplied: in that case move the corresponding rowset to position 0 // swapFirstInputRowSetIfExists( meta.getTargetXMLstep() ); } catch ( Exception e ) { log.logError( BaseMessages.getString( PKG, "XMLJoin.Error.Init" ), e ); return false; } return true; }
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)); String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) { meta = (AddXMLMeta) smi; data = (AddXMLData) sdi; if ( !super.init( smi, sdi ) ) { return false; } try { setSerializer( TransformerFactory.newInstance().newTransformer() ); setDomImplentation( XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder().getDOMImplementation() ); if ( meta.getEncoding() != null ) { getSerializer().setOutputProperty( OutputKeys.ENCODING, meta.getEncoding() ); } if ( meta.isOmitXMLheader() ) { getSerializer().setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" ); } } catch ( TransformerConfigurationException e ) { return false; } catch ( ParserConfigurationException e ) { return false; } return true; }
private void writeToFile(File template, Document document) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(template); transformer.transform(source, result); }
private void writeToFile(File template, Document document) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(template); transformer.transform(source, result); }
public static String toString(Document doc) { try { StringWriter sw = new StringWriter(); 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.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { try { Transformer serializer = tFactory.newTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if ( ! includeXmlDeclaration) { serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(out); serializer.transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new RuntimeException(e); } }
public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException { final Source source = new DOMSource(document); final Result resultat = new StreamResult(os); final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = fabrique.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, resultat); }
public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException { final Source source = new DOMSource(document); final Result resultat = new StreamResult(os); final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = fabrique.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, resultat); }
final public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException { final Source source = new DOMSource(document); final Result resultat = new StreamResult(os); final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = fabrique.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, resultat); }