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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
// 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
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
Sorry,
My typing mistake.