XSLT Transformation into String

M

Mathieu

Hello,

I 'd like to transform xml into another xml by using xslt.

My problem is that input xml and xslt are not in files but in strings.
Moreover output xml must be in string (because I use a database)

My code which doesn't work :

private String convertXML(String xml, String xslt) throws
TransformerException {

SAXResult xmlresult = new SAXResult();
Source xmlsource = new StreamSource(new
StringBufferInputStream(xml));
TransformerFactory xsltfactory = TransformerFactory.newInstance();
Transformer xsltengine = xsltfactory.newTransformer(new
StreamSource(new StringReader(xslt)));
xsltengine.transform(xmlsource,xmlresult);

return xmlresult.toString();
}




The result is bad

Have you got an idea ?

Thanks
 
T

Thomas Hawtin

Mathieu said:
I 'd like to transform xml into another xml by using xslt.

My problem is that input xml and xslt are not in files but in strings.
Moreover output xml must be in string (because I use a database)

Create a javax.xml.transform.stream.StreamResult with the Writer
constructor. Use CharArrayWriter as the Writer implementation, then
toString when you have finished.

Or more efficiently, go for a CharArrayReader. For large results, I
think you can use java.sql.Clob.setCharacterStream (which actually
returns a Writer).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top