Parsing multiple XML files, JDOM

I am creating a java class that will search a directory for XML files. If some of them are present, it will use JDOM to parse them and generate the simplified output described by XSLT. This will then be output to another directory, keeping the original XML name (ie Input XML is "sample.xml", XML output is also "sample.xml".

Currently, I can read in the specified XML and send the result in the specified XML, however, it won't make much sense in the future.

0


source to share


1 answer


Pass the directory argument to your program instead of the file argument. Then check that the passed argument is indeed a directory, displays all files, and processes 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());
        }
    }
}

      

+2


source







All Articles