tags:

views:

523

answers:

1

Hi folks,

I am creating a java class that will search a directory for xml files. If the are some present it will use JDOM to parse these and create a simplified output outlined by the xslt. This will then be output to another directory while retaining the name of the original xml i.e. input xml is "sample.xml", output xml is also "sample.xml".

At the moment I can read in a specified xml and send the result to a specified xml however this will not be of much/any use to me in the future.

I appreciate any help you could offer me.

Thanks!!

+1  A: 

Pass in a directory argument to your program, instead of a file argument. Then validate that the passed argument is really a directory, list all the files, and process each file. For example:

import java.io.File;
import java.io.FilenameFilter;

public class FileDemo {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            // print usage error
            System.exit(1);
        }

        File dir = new File(args[0]);
        if (!dir.isDirectory()) {
            // print usage error
            System.exit(1);
        }

        File[] files = dir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".xml");
            }
        });

        for (File file : files) {
            // process file
            System.out.println("File: " + file.getAbsolutePath());
        }
    }
}
Jason Day
Perfect solution, thanks for that Jason!
damien535