Home > other >  How to parse XML files without knowledge of filename
How to parse XML files without knowledge of filename

Time:04-21

I have a directory in which I receive orders as XML file. I want to parse this file and then do some things with it. I can set a scheduled job to check this directory every * seconds. I want to use this to parse the file:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new File("src/test/resources/example_jdom.xml"));
doc.getDocumentElement().normalize(); 

The problem is, I do not know the filename. I know the location where the XML file is going to appear, but I do not know how that file is going to be named.

How do solve this when I set my path? Since I can run my scheduled job every millisecond if I want, the chanches that 2 files appear at the exact same time is obsolete.

Thanks for your feedback.

CodePudding user response:

If you know the directory where the files end up, you can list it's content like so:

File dir = new File("/your/path");
System.out.println(dir.listFiles());

That should give you an idea of what is available for parsing. See also https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html#listFiles()

CodePudding user response:

Well a naive approach would be that you get the directly listing.

  1. Take the first file
  2. Look into the database if the file has already been processed
  3. If the filename does not exist, process it.
  4. After processing, add the filename and timestamp into the DB.
  5. Do the same for the next file.

If you are using Java 8, the following code can help you get the information from a directory.

  try (Stream<Path> walk = Files.walk(Paths.get("C:\\test"))) {
      result = walk
              .filter(p -> !Files.isDirectory(p))   // not a directory
              .map(p -> p.toString().toLowerCase()) // convert path to string
              .filter(f -> f.endsWith("png"))       // check end with
              .collect(Collectors.toList());        // collect all matched to a List
  }

Look at the following link for complete example: https://mkyong.com/java/how-to-find-files-with-certain-extension-only/

I mean you will find a lot of tutorials on how you can get the list of the filenames, you can adjust it accordingly, if you are interested only in XML files.

  • Related