Transformation : méthode la plus rapide
Bonjour,
Je dois transformer des fichiers XML à l'aide de feuilles XSLT de tranformation.
J'aurai à réaliser ceci pour une nombre très grand de fichiers XML.
Chaque fichier XML sera transformé par un ou plusieurs feuilles XSL.
Je ne connais pas à l'avance quelles seront les feuilles XSL à utiliser.
Tout ceci : ça va.
Pour l'instant j'utilise la méthode présentée ci-dessous.
Mon problème est de trouver la méthode qui me donnera les meilleurs performances.
Est-ce que cette méthode est la plus performante?
Merci de votre retour.
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
| import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class TestXslt {
public static void main(String[] args) {
String xmlFilePath = "test.xml";
String xslFilePath = "test.xsl";
String outFilePath = "resultat";
// On définit un transformer avec la source XSL qui va permettre la
// transformation.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = factory.newTransformer(new StreamSource(xslFilePath));
} catch (TransformerConfigurationException e) {
System.err.println("Impossible de créer un transformateur XML." + e);
System.exit(1);
}
// Get informations
String outputMethod = transformer.getOutputProperty("method");
System.out.println("method : " + outputMethod);
String outputEncoding = transformer.getOutputProperty("encoding");
System.out.println("encoding : " + outputEncoding);
// Création d'un fichier pour enregister le résultat de la génération.
File outFile = new File(outFilePath + "." + outputMethod);
System.out.println("Nom du fichier généré : " + outFile.getName());
FileOutputStream out = null;
try {
out = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
System.err.println("Impossible de créer le fichier résultat." + e);
System.exit(1);
}
// Transform
try {
transformer.transform(new StreamSource(new File(xmlFilePath)), new StreamResult(out));
} catch (TransformerException e) {
System.err.println("La transformation a échouée." + e);
System.exit(1);
}
System.out.println("Fichier généré avec succés.");
System.exit(0);
}
} |
Exemple: Un petit fichier XML de test:
Code:
1 2
| <?xml version="1.0"?>
<pipo>toto</pipo> |
Et une petite feuille XSLT de test:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:output method = "text"/>
<xsl:template match = "/">
<xsl:text>Version : </xsl:text>
<xsl:value-of select = "system-property('xsl:version')"/>
<xsl:text>
Vendor : </xsl:text>
<xsl:value-of select = "system-property('xsl:vendor')"/>
<xsl:text>
URL : </xsl:text>
<xsl:value-of select = "system-property('xsl:vendor-url')"/>
</xsl:template>
</xsl:stylesheet> |