/** * Transforms the given {@code Source} to the {@code Result}. * @param source the source to transform from * @param result the result to transform to * @throws TransformerException in case of transformation errors */ protected void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
private static String toString(org.w3c.dom.Node node) { try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(node); StringWriter out = new StringWriter(); transformer.transform(domSource, new StreamResult(out)); return out.toString(); } catch (TransformerException e) { throw new JSONException("xml node to string error", e); } }
/** * potentially unsafe XML transformation. * @param source The XML input to transform. * @param out The Result of transforming the <code>source</code>. */ private static void _transform(Source source, Result out) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // this allows us to use UTF-8 for storing data, // plus it checks any well-formedness issue in the submitted data. Transformer t = factory.newTransformer(); t.transform(source, out); }
private Transformer getTransformer() throws TransformerException { final TransformerFactory xformFactory = TransformerFactory.newInstance(); final Transformer transformer = xformFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); return transformer; }
private Transformer transformer(String xsltName, InputStream xslt) { try { return TransformerFactory.newInstance().newTransformer(new StreamSource(xslt)); } catch (TransformerConfigurationException tce) { throw bomb("Couldn't parse XSL template " + xsltName, tce); } }
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);
@Test public void whitespace() throws Exception { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>"; Transformer transformer = TransformerFactory.newInstance().newTransformer(); AbstractStaxXMLReader staxXmlReader = createStaxXmlReader( new ByteArrayInputStream(xml.getBytes("UTF-8"))); SAXSource source = new SAXSource(staxXmlReader, new InputSource()); DOMResult result = new DOMResult(); transformer.transform(source, result); Node node1 = result.getNode().getFirstChild().getFirstChild(); assertEquals(" ", node1.getTextContent()); assertEquals(" Some text ", node1.getNextSibling().getTextContent()); }
private void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
/**** * 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); }
@Before public void setUp() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); inputFactory = XMLOutputFactory.newInstance(); }
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();
/** * Transforms the given {@code Source} to the {@code Result}. * @param source the source to transform from * @param result the result to transform to * @throws TransformerException in case of transformation errors */ protected void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
@Before public void setUp() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); inputFactory = XMLInputFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilder = documentBuilderFactory.newDocumentBuilder(); }
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", "");
private void transformAndMarshal(Object graph, Result result) throws IOException { try { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); transformer.transform(new StreamSource(is), result); } catch (TransformerException ex) { throw new MarshallingFailureException( "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } }
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"))); }
/** * * @param file File to save Document to (ie AndroidManifest.xml) * @param doc Document being saved * @throws IOException * @throws SAXException * @throws ParserConfigurationException * @throws TransformerException */ private static void saveDocument(File file, Document doc) throws IOException, SAXException, ParserConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
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));
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); }