Traiter un ensemble de fichiers en boucle
Bonjour,
J'utilise xslt pour tranformer un xml plat vers un xml structuré. Le xml plat je l'obtiens suite à un parsing d'un ensemble de fichiers textes contenus dans différents répertoires.
Le problème c'est que si un fichier plante tout le traitement de mon appli plante (appel de XsltParser.parse(xml, xslt, output);). Donc je voudrais savoir si je peux faire tourner un xslt sur chacun des fichier en boucle comme:
for (File subfolder : directories) {
et comment concaténer par la suite les différents xml généré par le xslt ?
voici mon main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
public class Process {
public static void main(String[] args) {
new Process(args[0]);
}
/**
* @param folderName
* The folder path and name.
*/
public Process(String folderName) {
if (!folderName.isEmpty()) {
try {
// TODO Debug
System.out.println("Processus of: " + folderName);
// input file (flat xml) // cette classe met tt dans un seul fichier plat
Xmlprocess sub = new Xmlprocess(folderName);
StringBuffer flatXml = sub.serialize();
ByteArrayInputStream xml = new ByteArrayInputStream(flatXml.toString().getBytes());
// XSL template
InputStream xslt = getClass().getResourceAsStream("/xslt/transform.xslt");
// output file
FileOutputStream output = new FileOutputStream(folderName+ "\\out.xml" );
//
XsltParser.parse(xml, xslt, output);
output.close();
xslt.close();
xml.close();
// TODO Debug
System.out.println("Ouput XML file successfully written to: "+ folderName + "\\schema.xml");
s_logger.debug("Ouput XML file successfully written to: "+ folderName + "\\schema.xml");
} catch (IOException e) {
s_logger.error("This path doesn't exist or is invalid or is empty (check if it's a directory): " + folderName);
}
} else {
// TODO
s_logger.error("This path doesn't exist or is invalid or is empty (check if it's a directory): " + folderName);
// /e.printStackTrace();
}
}
} |
et voici ma classe Xmlprocess qui génère le xml plat
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public class Xmlprocess implements XmlSerializer {
/**
* The subscriptions root folder path.
*/
private String folderName;
public Xmlprocess(String folderName) {
this.folderName = folderName;
}
public StringBuffer serialize() throws XmlException {
StringBuffer xml = new StringBuffer();
File directory = new File(folderName);
if (directory.exists() && directory.isDirectory()) {
File[] list = directory.listFiles();
if (list != null) {
xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
// read all the folder content and process each parts
xml.append("<subscriptions>\n");
// find the subscriptions (subfolders)...
File[] directories = directory.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
});
// ... and process them
int numeroOrdre = 1;
for (File subfolder : directories) {
XmlParse xmls = new XmlParse(subfolder.getAbsolutePath(), numeroOrdre);
xml.append(xmls.serialize());
numeroOrdre++;
}
} else {
s_logger.debug("This path doesn't exist or is invalid or is empty (check if it's a directory): "+ folderName);
}
} else {
s_logger.debug("This path doesn't exist or is invalid or is empty (check if it's a directory): "+ folderName);
}
xml.append("</subscriptions>");
return xml;
}
} |
Merci infiniment