0

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.

1 Answer 1

2

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());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.