0

Possible Duplicate:
Applying XSLT v. 2 on XML

I have a Directory structure with XML files. I am having an XSLT 1.0 which I am applying on all these files and generating new XML files for each. I had written code in JAVA. But my problem is I am not able to put output files at a separate output folder having same structure as one from which I am taking my input XML file. For Example if I have a root directory Home with two folders Folder1 and Folder2. Each Folder1 & Folder2 has number of XML files. So when I convert my XML files present in these folders so the output files so generated should go in separate folder having same structure.

Here is the Java code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XMLwithXSLT {
  public static void main(String[] args) throws FileNotFoundException,    
    TransformerConfigurationException, TransformerException {

  File dir = new File("Input Directory Root Path Here");
  listFilesInDirectory(dir);
 }


 public  static void listFilesInDirectory(File dir) throws FileNotFoundException,     
       TransformerException {

   File[] files = dir.listFiles();
   if (files != null) {
     for (File f : files) {
       if (f.isDirectory()) {
         System.out.println(f.getName());
         listFilesInDirectory(f);
        } else {
        System.out.println(f.getName());
        OutputXml(f);

    }
   }
 }


    public static void OutputXml(File in) throws FileNotFoundException, 
    TransformerException{

     TransformerFactory tFactory = TransformerFactory.newInstance();
     Source xslDoc = new StreamSource("backup.xslt");
     Source xmlDoc = new StreamSource(in.getPath()) ; 
     System.out.print(in.getName() + "/n");
     String outputFileName =  in.getName();
     System.out.print(outputFileName );
     OutputStream htmlFile;
            htmlFile = new FileOutputStream(outputFileName);

     Transformer transformer = tFactory.newTransformer(xslDoc);
     transformer.transform(xmlDoc, new StreamResult(htmlFile));  
      }
   }  

So can anyone help me as how I can specify the output path for the generated new file? Also how I can generate a output files in the same directory format as input?

Community
  • 1
  • 1

2 Answers2

0

What i got from your question is , There are two folders Folder1 and Folder2. Folder1 Have some XML file and Folder2 have some XML file. Lets consider you have got the output for all xml file present in Folder1 to HTML format. and Now you want those HTML files to be present in a folder called Folder1 some where in your system(or lets consider the temp folder of the system).and same for all the XML files present in Folder2.

If you want this kind of output then update the code with following



    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;

    public class XMLwithXSLT {
      static String pathRequiredForFile=null;
      static String tempfolder=System.getProperty("java.io.tmpdir");
      public static void main(String[] args) throws FileNotFoundException,    
        TransformerConfigurationException, TransformerException {

      File dir = new File("Input Directory Root Path Here");
      listFilesInDirectory(dir);
     }
     public  static void listFilesInDirectory(File dir) throws FileNotFoundException,     
           TransformerException {

       File[] files = dir.listFiles();


       if (files != null) {
         for (File f : files) {
           if (f.isDirectory()) {
               pathRequiredForFile=f.getName();

             listFilesInDirectory(f);

            } else {
            System.out.println(f.getName());
            File path=new File(tempfolder+"//"+pathRequiredForFile);
            path.mkdir();
            OutputXml(f,path.getAbsolutePath());

        }
       }
     }
     }
        public static void OutputXml(File in,String saveFileInPath) throws FileNotFoundException, 
        TransformerException{

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Source xslDoc = new StreamSource("backup.xslt");
        Source xmlDoc = new StreamSource(in.getPath()) ; 
        System.out.print(in.getName() + "/n");
         String outputFileName = in.getName().split("\\.")[0];
         System.out.println(outputFileName );
         OutputStream htmlFile;
                htmlFile = new FileOutputStream(saveFileInPath+"//"+outputFileName+".html");

         Transformer transformer = tFactory.newTransformer(xslDoc);
         transformer.transform(xmlDoc, new StreamResult(htmlFile));  
          }
       } 

Rakesh Mahapatro
  • 866
  • 5
  • 12
0

You should add the path to your output dir. For example:

String outputFileName = "c:\\tmp\\xmloutput\\" + in.getName();

So basicaly, loop your input files and take over subdir and file name. See if it exists in your output directory and if not create it. And if zo, then name your outputFileName this way.

roel
  • 2,005
  • 3
  • 26
  • 41