All - not sure if I'm on the right track with this or not, but here's what I have so far.
We have a processor that needs to go through and handle a series of files we specify in a config file.
XMLConfiguration config = new XMLConfiguration(CONFIG_FILE_DIR);
if (config != null) {
String filePath = config.getString("files.file.filepath");
String inputFile = config.getString("files.file.inputfile");
String outputFile = config.getString("files.file.outputfile");
String cellRange = config.getString("files.file.cellrange");
translateXLS(filePath, inputFile, outputFile, cellRange);
}
else {
System.out.println("CONFIG NOT SET");
}
The translate XLS method works great, and goes ahead and processes whatever file we gave it from a config file.
We have a bit of a problem when we want it to process multiple files though, as it will just simply take the last entry and use that. Obviously I don't have any looping mechanisms or arrays in place so that makes sense, but I am wondering if I need to use XPath or something along those lines to handle a sample config below:
<xml version="1.0" encoding="UTF-8">
<files>
<file>
<filepath>C:\sample\</filepath>
<inputfile>1.xls</inputfile>
<outputfile>1out.out</outputfile>
<cellrange>F1:I2</cellrange>
</file>
<file>
<filepath>C:\sample\</filepath>
<inputfile>2.xls</inputfile>
<outputfile>2out.out</outputfile>
<cellrange>A1:J2</cellrange>
</file>
</files>
</xml>
What would be the best option for storing something like a list or array of configs to loop through and perform actions on all files within the same config.xml?
I have mainly been using this for a reference. Technically I can set up something like the indexes they use here, but I would have to hard code exactly how many entries I have. It'd be nice if it were flexible enough to just go through every entry.
http://www.code-thrill.com/2012/05/configuration-that-rocks-with-apache.html
You can use JAXB if you don't want to have any external dependencies. JAXB is included in Java 6 and later.
First create a class that represents a <file></file>
entry:
public class FileEntry {
@XmlElement public String filepath;
@XmlElement public String inputfile;
@XmlElement public String outputfile;
@XmlElement public String cellrange;
}
Then create a class representing <files></files>
:
public class Config {
@XmlElement(name="file") public List<FileEntry> entries;
}
Then load the XML:
public static void main(String[] args) throws JAXBException {
JAXBContext ctx = JAXBContext.newInstance(Config.class);
Unmarshaller um = ctx.createUnmarshaller();
JAXBElement<Config> element = um.unmarshal(new StreamSource(new File("config.xml")), Config.class);
Config config = element.getValue();
for (FileEntry file : config.entries) {
System.out.println(file.filepath);
System.out.println(file.inputfile);
System.out.println(file.outputfile);
System.out.println(file.cellrange);
}
}
And the first line of your xml file should be <?xml version="1.0" encoding="UTF-8"?>
and no </xml>
at the end.