XSLT Transformation into String

Discussion in 'Java' started by Mathieu, Jun 1, 2006.

  1. Mathieu

    Mathieu Guest

    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
    Mathieu, Jun 1, 2006
    #1
    1. Advertising

  2. Mathieu wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jun 1, 2006
    #2
    1. Advertising

  3. Mathieu

    Mathieu Guest

    Thanks very very much.

    My first test works ;)

    Thomas Hawtin a écrit :
    > Mathieu wrote:
    >>
    >> 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
    Mathieu, Jun 1, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0Qw==?=

    Xslt Transformation getting < and >

    =?Utf-8?B?UGF0Qw==?=, May 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    852
    Steven Cheng[MSFT]
    May 19, 2004
  2. clogwog
    Replies:
    0
    Views:
    413
    clogwog
    Oct 20, 2004
  3. Blue Gecko
    Replies:
    1
    Views:
    433
    Blue Gecko
    Oct 3, 2005
  4. pradeep gummi
    Replies:
    3
    Views:
    954
    pradeep gummi
    Aug 13, 2003
  5. Jon Martin Solaas
    Replies:
    2
    Views:
    448
    Jon Martin Solaas
    Jun 15, 2004
Loading...

Share This Page