damien535
damien535

Reputation: 637

Parse multiple xml files, JDOM

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.

Upvotes: 0

Views: 2317

Answers (1)

Jason Day
Jason Day

Reputation: 8839

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());
        }
    }
}

Upvotes: 2

Related Questions