Help with SAXSource and javax.xml.transform.Transformer

Discussion in 'Java' started by Andreas, Sep 2, 2005.

  1. Andreas

    Andreas Guest

    Hi!

    I fail to catch events when transforming xml.
    I have some code that successfully transforms an xml

    ... code ...
    // Setup XSL transformer
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(xslStreamSource);

    // Setup XML result
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    StreamResult transformResult = new StreamResult(bos);

    // Do the transform
    StreamSource ss = new StreamSource(source);
    transformer.transform(ss, transformResult);
    ... end code ...

    Now I need this code to perform some "side effects" when parsing the
    original xml. I know it is possible to catch events in a ContentHandler
    when parsing xml with XMLReader. I tried this with the following code.

    ... code ...
    // Perform Sideeffects
    InputSource is = new InputSource(source);
    MyContentHandler mch = new MyContentHandler();
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(sch);
    reader.parse(is);
    ... end code ...

    Ok, so know I know that XMLReader.parse will fire events. Why can't I
    use this reader with a SAXSource to catch these events when doing the
    transform?

    Something like:

    ... code ...
    SAXSource ss = new SAXSource(reader, is);
    transformer.transform(ss, transformResult);
    ... end code ...

    Unfortunately this code doesn't produce any events

    Any solution for this problem?

    /Andreas
     
    Andreas, Sep 2, 2005
    #1
    1. Advertising

  2. Andreas wrote:
    >
    > Something like:
    >
    > ... code ...
    > SAXSource ss = new SAXSource(reader, is);
    > transformer.transform(ss, transformResult);
    > ... end code ...
    >
    > Unfortunately this code doesn't produce any events
    >
    > Any solution for this problem?
    >


    Perhaps if you posted a short, self-contained code example (SSCCE) it
    would be easier to help you.

    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Sep 4, 2005
    #2
    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. veny
    Replies:
    0
    Views:
    542
  2. Replies:
    1
    Views:
    754
    Esmond Pitt
    Mar 27, 2005
  3. Abdelhalim
    Replies:
    2
    Views:
    1,057
    Tom Hawtin
    Jul 6, 2007
  4. Replies:
    1
    Views:
    391
    John B. Matthews
    Jan 12, 2009
  5. Aéris
    Replies:
    4
    Views:
    1,355
    Arne Vajhøj
    Oct 12, 2011
Loading...

Share This Page