transformation de xml avec xsl
bonjour j'ai un fichier xml que je le transforme avec un fichier xsl en fichier htm
ce dernier fichier htm se crée dans l'endroit que je lui indique
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
package beanAction;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.transform.JDOMResult;
import com.opensymphony.xwork2.ActionSupport;
public class AfficherDocAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String titre;
private String createur;
private String sujet;
private String description;
private String date;
static Element racine = new Element("documents");
static Document document = new Document(racine);
public String execute() throws Exception {
// On cr�e une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try {
Document jDomDoc = sxb.build(new File("C:/workspace/GED-1.1/ressources/documents.xml"));
outputXSLT(jDomDoc, "C:/workspace/GED-1.1/ressources/Doc.xsl");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// Exception si le fichier est mal form�
private void outputXSLT(Document jDomDoc, String fichierXSL) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
/* Document JDOMResult, résultat de la transformation TraX */
JDOMResult documentJDOMSortie = new JDOMResult();
/* Document JDOM après transformation */
Document resultat = null;
try {
/*
* On définit un transformer avec la source XSL qui va permettre la
* transformation
*/
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
fichierXSL));
/*
* On transforme le document JDOMEntree grâce à notre transformer.
* La méthoded transform() prend en argument le document d'entree
* associé au transformer et un document JDOMResult, résultat de la
* transformation TraX
*/
transformer.transform(new org.jdom2.transform.JDOMSource(jDomDoc),
documentJDOMSortie);
/*
* Pour récupérer le document JDOM issu de cette transformation, il
* faut utiliser la méthode getDocument()
*/
resultat = documentJDOMSortie.getDocument();
/* On crée un fichier xml corespondant au résultat */
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
System.out.println();
// outputter.output(resultat, new FileOutputStream(
// "C:/workspace/GED-1.1/WebContent/jsp/resultat.htm"));
} catch (Exception e) {
}
}
} |
mais moi je veux que ce fichier htm s'affiche directement sur eclipse dans une page htm