java: make (xslt) transformer throw exception on error

K

killy971

I am using the following syntax, with Xalan, to process xml
transformations in java:

StreamSource xml = new StreamSource(xmlFile);
StreamSource xsl = new StreamSource(xslFile);
StreamResult out = new StreamResult(outputFile);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xsl);
transformer.transform(xml, out);

If the xslt sheet input is broken (invalid xml, etc.), an exception is
thrown (fatal error occurs), but if the input xml file (file to
transform) is broken no exception is thrown (because it is not a fatal
error).
I would like to knwo if there is a way to make the transformer throw
an exception on this kind of errors, or if there is a way to check if
an error has occured after the "transform()" call.
 
J

Johannes Koch

killy971 said:
I am using the following syntax, with Xalan, to process xml
transformations in java:

StreamSource xml = new StreamSource(xmlFile);
StreamSource xsl = new StreamSource(xslFile);
StreamResult out = new StreamResult(outputFile);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xsl);
transformer.transform(xml, out);

If the xslt sheet input is broken (invalid xml, etc.), an exception is
thrown (fatal error occurs), but if the input xml file (file to
transform) is broken no exception is thrown (because it is not a fatal
error).

Parse the xmlFile with a DocumentBuilder, then pass a DOMSource with the
resulting Document to the transform method. In case the xmlFile is not
wellformed you'll get an Exception from the DocumentBuilder.
 

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,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top