public void marshal(Object zip, Result result) throws IOException, XmlMappingException { String xmlString = "<weat:GetCityWeatherByZIP xmlns:weat=\"http://ws.cdyne.com/WeatherWS/\">" + " <weat:ZIP>" + zip + "</weat:ZIP>" + "</weat:GetCityWeatherByZIP>"; try { Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new StringSource(xmlString), result); } catch (Exception e) { e.printStackTrace(); } }
public static String docAsString(Document doc) { try { StringResult res = new StringResult(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(doc), res); return res.toString(); } catch (Exception e) { throw new RuntimeException(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() {
@Test public void testWebServiceRequestAndResponse() { StringResult result = new StringResult(); Source payload = new StringSource( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<echoRequest xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoRequest>"); template.sendSourceAndReceiveToResult(WS_URI, payload, result); logger.info("RESULT: " + result.toString()); assertThat(result.toString(), equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<echoResponse xmlns=\"http://www.springframework.org/spring-ws/samples/echo\">hello</echoResponse>")); } }
public XPathMessageSplitter(String expression, Map<String, String> namespaces) { this(expression, namespaces, TransformerFactoryUtils.newInstance()); }
@Override public void marshal(Object source, Result result) throws XmlMappingException, IOException { if (source instanceof Message) { this.messages.add((Message<?>) source); } else { this.payloads.add(source); } if (result instanceof StringResult) { ((StringResult) result).getWriter().write("hello " + source); } }
private String transformString(String stringPayload, Transformer transformer) throws TransformerException { StringResult result = new StringResult(); Source source; if (this.alwaysUseSourceFactory) { source = this.sourceFactory.createSource(stringPayload); } else { source = new StringSource(stringPayload); } transformer.transform(source, result); return result.toString(); }
public static String sourceToString(Source source) throws Exception { StringResult res = new StringResult(); transform(source, res); return res.toString(); } }
/** * Prettifies XML data, e.g. add indentation. * * @param result The XML to prettify * @return Prettified XML * @throws Exception */ public static String formatXml(String result) throws Exception{ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2)); StreamResult streamResult = new StreamResult(new StringWriter()); Source source = new StringSource(result); transformer.transform(source, streamResult); String xmlString = streamResult.getWriter().toString(); return xmlString; }
public Result createResult(Object payload) { return new StringResult(); }
public StringSourceFactory() { this(TransformerFactoryUtils.newInstance()); }
private StringSource createStringSourceForDocument(Document document) { try { StringResult result = new StringResult(); Transformer transformer = getTransformer(); transformer.transform(new DOMSource(document), result); return new StringSource(result.toString()); } catch (Exception e) { throw new MessagingException("failed to create StringSource from document", e); } }
public static String docToString(Document doc) throws Exception { DOMSource source = new DOMSource(doc); StringResult stringResult = new StringResult(); transform(source, stringResult); return stringResult.toString(); }
private Templates getXslTemplates() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + " <xsl:template match=\"order\">" + " <bob>test</bob>" + " </xsl:template>" + "</xsl:stylesheet>"; return transformerFactory.newTemplates(new StringSource(xsl)); }
public static StringResult getStringResultForString(String strDoc) throws Exception { StringResult res = new StringResult(); transform(getDomSourceForString(strDoc), res); return res; }
public XPathMessageSplitter(XPathExpression xpathExpression) { this(xpathExpression, TransformerFactoryUtils.newInstance()); }
private StringSource createStringSourceForFile(File file) { try { String content = FileCopyUtils.copyToString(new FileReader(file)); return new StringSource(content); } catch (Exception e) { throw new MessagingException("failed to create StringSource from file", e); } }
public ResultToStringTransformer() { this.transformerFactory = TransformerFactoryUtils.newInstance(); }
public void marshal(Object graph, Result result) throws XmlMappingException, IOException { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringSource stringSource = new StringSource("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root>" + graph.toString() + "</root>"); transformer.transform(stringSource, result); } catch (Exception e) { throw new RuntimeException(e); } }
public void marshal(Object graph, Result result) throws XmlMappingException, IOException { Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); StringSource stringSource = new StringSource(graph.toString()); transformer.transform(stringSource, result); } catch (Exception e) { throw new MarshallingFailureException("Stub failed to marshal", e); } }