/** * 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 static String transform(Transformer transformer, Node element) throws TransformerException { StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(element); transformer.transform(source, result); return result.getWriter().toString(); }
private String upgrade(String originalContent, URL upgradeScript) { try (InputStream xslt = upgradeScript.openStream()) { ByteArrayOutputStream convertedConfig = new ByteArrayOutputStream(); transformer(upgradeScript.getPath(), xslt) .transform(new StreamSource(new ByteArrayInputStream(originalContent.getBytes())), new StreamResult(convertedConfig)); return convertedConfig.toString(); } catch (TransformerException e) { throw bomb("Couldn't transform configuration file using upgrade script " + upgradeScript.getPath(), e); } catch (IOException e) { throw bomb("Couldn't write converted config file", e); } }
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() {
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);
private void transform(Document doc) { DOMSource source = new DOMSource(doc); this.setWriter(new StringWriter()); StreamResult result = new StreamResult(this.outputWriter); try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } }
@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); }
@Test public void streamReaderSourceToStreamResult() throws Exception { XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML)); StaxSource source = new StaxSource(streamReader); assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader()); assertNull("EventReader returned", source.getXMLEventReader()); StringWriter writer = new StringWriter(); transformer.transform(source, new StreamResult(writer)); assertThat("Invalid result", writer.toString(), isSimilarTo(XML)); }
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();
public void createXml(OutputStream os) throws TransformerException, ParserConfigurationException { final DOMSource source = new DOMSource(getDocument()); final StreamResult scrResult = new StreamResult(os); getTransformer().transform(source, scrResult); }
/** * 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); }
@Test public void eventReaderSourceToStreamResult() throws Exception { XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML)); StaxSource source = new StaxSource(eventReader); assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader()); assertNull("StreamReader returned", source.getXMLStreamReader()); StringWriter writer = new StringWriter(); transformer.transform(source, new StreamResult(writer)); assertThat("Invalid result", writer.toString(), isSimilarTo(XML)); }
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));