Java Saxon Parsing

869 views Asked by At

I'm using Saxon parser to split the big file into smaller ones. Below is my sample code,

  TransformerFactory tFactory = TransformerFactory.newInstance();
           Transformer transformer = tFactory
                    .newTransformer(new StreamSource(new File(xsltPath)));
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            transformer.transform(new StreamSource(new File(sourcePath)),
                    new StreamResult(new File(resultDir)));

Where sourcePath = C:/path/Temp/AppModule.xml xsltPath = C:/path/Temp/create-fragment.xslt resultDir = C:/path/Temp/

This code splits the AppModule.xml into smaller xml files perfectly but with exception in the console,

Error java.io.FileNotFoundException: C:\path\Temp (Access is denied) net.sf.saxon.trans.XPathException: java.io.FileNotFoundException: C:\path\Temp (Access is denied)

I googled and found that I should specify the exact file name to new File() method. But as you see, the file name i do not know at compile time, only during run time the parser identifies the input AppModule.xml and split the xml into smaller files with the name of value tag in it.

AppModule.xml

    <?xml version='1.0' encoding='UTF-8'?>
<data>
<value>A1</value>
<value>B1</value>
<value>C1</value>
<value>A2</value>
<value>B2</value>
<value>C2</value>
</data>

OutPut: A1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<test>A1</test>

Similarly it will create B1,c1,A2,B2,C2 files correspondingly.

Please share your valuable comments.

1

There are 1 answers

0
Michael Kay On BEST ANSWER

The file you give in the result object should not be a directory. If the transformation produces no "primary" output file, but only produces output using xsl:result-document, then you should specify the result file as something like new File("c:/path/temp/dummy.xml"). This file will be used as the "base output URI" for resolving any relative filename supplied in xsl:result-document/@href.

The API you are using is called JAXP, and the problem is that it was designed for XSLT 1.0, where you could only have one result document. Saxon has attempted to stretch the concepts in JAXP to make it work with XSLT 2.0, but it's not really designed for the job. You might like to look at Saxon's s9api interface as an alternative.