Traiter un ensemble de fichiers en boucle avec XSLT

caro_perf Messages postés 29 Date d'inscription dimanche 6 mars 2005 Statut Membre Dernière intervention 13 mars 2007 - 12 mars 2007 à 22:04
caro_perf Messages postés 29 Date d'inscription dimanche 6 mars 2005 Statut Membre Dernière intervention 13 mars 2007 - 13 mars 2007 à 00:32
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. Donc je voudrais savoir si je peux faire tourner un xslt
en boucle telque:

for (File subfolder : directories) {

et comment concaténer par la suite les différents xml généré par le xslt ?

voici mon main :

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

<!-- END TEMPLATE: bbcode_code -->


et voici ma classe Xmlprocess qui génère le xml plat

<!-- BEGIN TEMPLATE: bbcode_code -->

Code :

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("<?xmlversion="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;
}
}
 

<!-- END TEMPLATE: bbcode_code -->

Merci infiniment

1 réponse

caro_perf Messages postés 29 Date d'inscription dimanche 6 mars 2005 Statut Membre Dernière intervention 13 mars 2007
13 mars 2007 à 00:32
heeeeeeeeeeeeeeeeeeeeeelp
0
Rejoignez-nous