简体   繁体   中英

How to read multiple XML files in Java?

I have a directory which contains several XML files in the similar format. Below is one example

<students>
    <forename>Joe</forename>
    <surname>Bloggs</surname>
    <DOB>01/01/1970</DOB>
    <educationLevel>High School</educationLevel>
</students>

Is there a way to iterate through the directory containing the xml files(maybe in some sort of loop?) and read the xml files one by one?

Get the XML files residing directory

  • Get the no of .xml files present in the specific directory (validate the extension of each file for this operation so that other file formats are eliminated)
  • Then put it in the loop and iterate over each file and parse them according to your parser (DOM, SAX, JAXB etc.,) and save it in required format

You could use the following approach to iterate through .xml files in the given directory:

public static void readFiles(String directory) throws IOException {
    File dir = new File(directory);
    if (dir.exists() && dir.isDirectory()) {
        File [] files = dir.listFiles((d, name) -> name.endsWith(".xml"));
        if (files != null) {
            for (File file: files) {
                String xml = new String(Files.readAllBytes(file.toPath()), 
                                        Charset.defaultCharset());
                //parse xml
            }
        }
    }
}

And to parse XML you could use, for example, Jackson Object Mapper :

ObjectMapper xmlMapper = new XmlMapper();
YourPOJO value = xmlMapper.readValue(xml, YourPOJO.class);

Retrieve all the student tags in an array for example in python I wanted to get all divs with a specific id:

link4 = "Any HTML Link"

data3 = requests.get(link4)

soup5 = BeautifulSoup(data3.text, "lxml")

soup6 = BeautifulSoup(str(soup5.findAll("div", { "id" : "specs-list" })),"lxml")

After this, you can iterate over soup6 turn by turn

I ended up doing this:

public static void directoryLoop(File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            directoryLoop(fileEntry);
        } else {
            xmlReader(fileEntry.getName());
        }
    }
}

where xmlReader(fileEntry.getName()); is my method for reading the XML files which takes the file name as a parmeter.

assuming that you have the reading of the xmlfile already implemented and that you know your filenames you could do something like this:

List<String> filenames;

for(String filename : filenames){
    myXmlReadingMethod( "path of my Files " + filename);
}

I hope this helps you abit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM