Menu

SAXResult vs TransformerHandler

Help
2001-12-17
2012-10-08
  • Brett Knights

    Brett Knights - 2001-12-17

    Hi,

    I am trying to use a ContentHandler with Saxon. I can use a TransformerHandler similar to the the exampleContentHandlerToContentHandler example in the TraxExamples.java file that comes with Saxon.

    What I wanted to do however was just call transform like:
    transformer.transform(source, new SAXResult(new ThingThatExtendsDefaultHandler()));

    When I try that the only SAX events that ever get fired in my ThingThatExtendsDefaultHandler are character events telling me about newlines.

    Should this work? If not would someone please direct me to the spec that says why not.

     
    • Michael Kay

      Michael Kay - 2001-12-18

      Yes, it should work: though I'm a little surprised to discover this case isn't included in TraxExamples. If you think it isn't working, I'd like to have the evidence, please.

      Mike Kay

       
    • Michael Kay

      Michael Kay - 2001-12-18

      I've added a SAXResult test to TraxExamples which shows it is working OK (for me). The code is:

          public static void exampleSAXResult(
                  String sourceID, String xslID)
                      throws TransformerException,
                             TransformerConfigurationException, SAXException,
                             IOException, MalformedURLException {

              TransformerFactory tfactory = TransformerFactory.newInstance();

              // Does this factory support SAX features?
              if (tfactory.getFeature(SAXResult.FEATURE)) {

                  // Get a transformer in the normal way:
                  Transformer transformer =
                      tfactory.newTransformer(new StreamSource(xslID));

                  // Get the source as a StreamSource
                  Reader       xmlReader = new BufferedReader(new FileReader(sourceID));
                  StreamSource xmlSource = new StreamSource(xmlReader);

                  // Set the result handling to be a serialization to System.out.
                  Result result = new SAXResult(new ExampleContentHandler());

                  // Do the transformation
                  transformer.transform(xmlSource, result);
                 
              } else {
                  System.out.println(
                      "Can't do exampleSAXResult because tfactory is not a SAXTransformerFactory");
              }
          }

      The ExampleContentHandler shows that all events are being notified correctly.

      Mike Kay

       
    • Brett Knights

      Brett Knights - 2001-12-18

      Sorry,

      My typing mistake.