Creation de plusieurs page HTML
Bonjour, voila mon code java permettant de créer une page html via un XML et mis en forme par un XSL.
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 51 52 53 54
| public class Main {
public static void main(String[] args) {
try {
System.out.println("Go");
//construction du fichier XML DocumentBuilderFactoryfabrique=DocumentBuilderFactory.newInstance();
fabrique.setNamespaceAware(true);
// fabrique.setValidation(true);
DocumentBuilder analyseur =fabrique.newDocumentBuilder();
//création du fichier XML
Document doc=analyseur.parse(newFile("FR-FR-Form270207-4-1.xml"));
DOMSource sourceXML = new DOMSource(doc);
//Création du fichier XSL
doc = analyseur.parse(new File("test.xsl"));
DOMSource sourceXSL = new DOMSource(doc);
TransformerFactory trFact = TransformerFactory.newInstance();
Transformer transformeur = trFact.newTransformer(sourceXSL);
transformeur.setOutputProperty(OutputKeys.METHOD, "html");
transformeur.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformeur.setOutputProperty(OutputKeys.INDENT, "yes");
//transformeur.setParameter("num", "6");
File oFic = new File("resultat.vm");
FileOutputStream fos = new FileOutputStream(oFic);
if (fos != null) {
Result sortie = new StreamResult(fos);
transformeur.transform(sourceXML, sortie);
}
fos.flush();
fos.close();
System.out.println("XSL transf done");
Velocity.init();
VelocityContext context = new VelocityContext();
context.put( "date", new DateHelper());
Template template = Velocity.getTemplate("resultat.vm");
FileWriter fw = new FileWriter("result.html");
template.merge( context, fw );
fw.close();
System.out.println("Velocity transf done");
} catch (Throwable t) {
t.printStackTrace();
}
}
} |
Je souhaite créer à partir de ce XML plusieurs page HTML, mais ne sait pas comment faire.
Merci d'avance ;)
Traitement de plusieurs XML
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 51 52 53 54 55 56 57 58 59 60 61 62
|
public static void maMethode(){
try {
System.out.println("Go");
//Selection des fichiers XML à traiter
File file = new File("");
String chemin = file.getAbsolutePath();
File oChemin = new File(chemin);
File[] oCollection = oChemin.listFiles();
for(int i = 0; i < oCollection.length; i++){
if (oCollection[i].getName().endsWith(".xml"))
System.out.println("Nom de la cible : " + oCollection[i].getName());
//construction du fichier XML
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
fabrique.setNamespaceAware(true);
DocumentBuilder analyseur = fabrique.newDocumentBuilder();
//création du fichier XML
Document doc = analyseur.parse(new File(oCollection[i].getName()));
DOMSource sourceXML = new DOMSource(doc);
//Création du fichier XSL
doc = analyseur.parse(new File("test.xsl"));
DOMSource sourceXSL = new DOMSource(doc);
TransformerFactory trFact = TransformerFactory.newInstance();
Transformer transformeur = trFact.newTransformer(sourceXSL);
transformeur.setOutputProperty(OutputKeys.METHOD, "html");
transformeur.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformeur.setOutputProperty(OutputKeys.INDENT, "yes");
//transformeur.setParameter("num", "6");
File oFic = new File("resultat"+i+".vm");
FileOutputStream fos = new FileOutputStream(oFic);
if (fos != null) {
Result sortie = new StreamResult(fos);
transformeur.transform(sourceXML, sortie);
}
fos.flush();
fos.close();
System.out.println("XSL transf done");
Velocity.init();
VelocityContext context = new VelocityContext();
context.put( "date", new DateHelper());
Template template = Velocity.getTemplate("resultat"+i+".vm");
FileWriter fw = new FileWriter("result"+i+".xls");
template.merge( context, fw );
fw.close();
System.out.println("Velocity transf done");
}
} catch (Throwable t) {
t.printStackTrace();
}
} |
J'essaye de traiter tous les fichiers XML présent dans mon répertoire courant et de créer a chaque fois un fichier excel (resultat du traitement). Je ne comprend pas pourquoi ca plante....
Le resulat est le traitement de 3 fichier XML sur 6 , et avec un fichier excel en sortie qui bug
Merci d'avance !!
Merci d'avance ;)