0

I downloaded the Saxon 10 samples from the Saxonica website to get some examples on how to use the s9api.

I found all samples using the classic java.io.* classes to read and write files for the XSL transformation. I then tried to find any Saxon examples using the newer Java NIO and NIO.2 packages for I/O**.

But I had surprisingly no luck at all. And I wonder why.

To be more concrete, let's focus on two examples:

Does it make a real difference if I use the java.nio.file.Files class instead of classic java.io.File to tell the Serializer in what file to save the transformation results?

Path targetFile ...
transformer.newSerializer(Files.newOutputStream(targetFile));
transformer.newSerializer(Files.newBufferedWriter(targetFile));
transformer.newSerializer(targetFile.toFile()); // java.io.File

Or, on the input side, is one of the following better than the others?

Path targetFile ...    
transformer.transform(new StreamSource(Files.newInputStream(xml)), out);
transformer.transform(new StreamSource(Files.newBufferedReader(xml)), out);
transformer.transform(new StreamSource(xml.toFile()), out); // java.io.File

Or is the parameter type just a "file reference" and the Serializer and the StreamSource do their best no matter how I tell them what file to use.

burki
  • 6,741
  • 1
  • 15
  • 31
  • Are there methods like `Files.newBufferedReader(xml)` and `Files.newBufferedWriter(targetFile)` that don't require you to provide a character set? But in any case, for XML, if you want the XML parser to detect the encoding/charset I would recommend to open a File or a Stream. – Martin Honnen Aug 20 '21 at 10:24
  • 1
    Most of the samples were written many years ago. They're tested at every new release but we haven't made much attempt to modernise them (that's partly because we try to support old versions of Java wherever possible: the current baseline is Java 8). Obviously if an API expects an InputStream or Reader then it really doesn't matter where it comes from or how it's constructed. – Michael Kay Aug 20 '21 at 11:15

0 Answers0