0

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 chances that 2 files appear at the exact same time is negligible.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Yannick Mussche
  • 316
  • 2
  • 12
  • Does this answer your question? [Watching a Directory for Changes in Java](https://stackoverflow.com/questions/23452527/watching-a-directory-for-changes-in-java) – Ken Y-N Apr 21 '22 at 08:15
  • It sounds like what you're *actually* asking is "How do I get the files in a directory?" `File.listFiles()` would be one starting point - there may be more modern Path-based approaches too. – Jon Skeet Apr 21 '22 at 08:16
  • @YannickMussche You can do listFIles and then iterate over it - search for file/s with .xml extension/s. – Wortig Apr 21 '22 at 08:22
  • @KenY-N, I have studied the link you posted. I do not think it answers my question. Because it explains: // We obtain the file system of the Path, but I know the file system of the path, I do not know the name of the file that has to be parsed in that directory. Or does this path also include the name of the xml file? – Yannick Mussche Apr 21 '22 at 08:24
  • @Wortig, I think this might be it. To be clear, this method only makes a list of the filenames in the directory. It does not already the xml data ? I still have to use the code above to then read the xml files, of which i acquired the names? – Yannick Mussche Apr 21 '22 at 08:29
  • AFAIK, the `Path` here means the file plus (relative, it seems) path , not the path alone. – Ken Y-N Apr 21 '22 at 08:38

2 Answers2

0

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

Queeg
  • 7,748
  • 1
  • 16
  • 42
0

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.

office.aizaz
  • 179
  • 11