IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Format d'échange (XML, JSON...) Java Discussion :

transformer un xml en html (en utilisant java) [DOM]


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut transformer un xml en html (en utilisant java)
    Bonjour tout le monde,
    Voilà suite au dernier sujet que j'ai postulé qui consiste à transformer un xml en pdf, je souhaiterai maintenant transformer un xml en html.

    J'ai essayé avec ça, mais j'ai une petite erreur que j'arrive pas à trouver un explication.

    Voilà le premier code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.io.File;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
     
    import org.w3c.dom.Document;
     
     
    public  class XSLTRANSFORM {
     
     
    	public static void creerHTML(String xml, String xsl, String html) throws Exception
    	{
    		// Création de la source DOM
    		DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
     
    		// creation d'un constructeur
    		DocumentBuilder constructeur = Factory.newDocumentBuilder();
    		File fileXml = new File(xml);
    		Document document = constructeur.parse(fileXml);
            Source source = new DOMSource(document);
     
            // Création du fichier de sortie
            File fileHtml = new File(html);
            Result resultat = new StreamResult(fileHtml);
     
            // Configuration du transformer
            TransformerFactory fabriqueT = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(xsl);
            Transformer transformer = fabriqueT.newTransformer(stylesource);
            transformer.setOutputProperty(OutputKeys.METHOD, "html");
     
            // Transformation
            transformer.transform(source, resultat);
    	}
     
     
        public static void main(String [] arg) throws TransformerException{
     
        	// Setup directories
    		File baseDir = new File("C:/CreationHTML/");
    		File outDir = new File(baseDir, "out");
    		outDir.mkdirs();
     
    		//Setup input and output files          
    		File xmlFile = new File(baseDir, "Annuaire.xml");
    		File xsltFile = new File(baseDir, "Annuaire.xsl");
    		File htmlFile = new File(outDir, "Annuaire.html");
     
     
            javax.xml.transform.Source xmlSource =
                    new javax.xml.transform.stream.StreamSource(xmlFile);
            javax.xml.transform.Source xsltSource =
                    new javax.xml.transform.stream.StreamSource(xsltFile);
            javax.xml.transform.Result result =
                    new javax.xml.transform.stream.StreamResult(htmlFile);
     
     
            javax.xml.transform.TransformerFactory transFact =
                    javax.xml.transform.TransformerFactory.newInstance();
     
            javax.xml.transform.Transformer trans =
                    transFact.newTransformer(xsltSource);
     
            trans.transform(xmlSource, result);
     
     
     
            }
     
    }
    Le deuxième code (deuxième proposition -> enfin de compte c'est pas très different du premier mais c'est juste pour vous dire que j'ai la meme erreur avec ses deux codes.)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import javax.xml.parsers.*; 
    import org.w3c.dom.*; 
    import javax.xml.transform.*;  
    import javax.xml.transform.dom.*; 
    import javax.xml.transform.stream.*; 
    import java.io.*; 
     
    public class CreationHTML{
    	public static void creerHTML(String xml, String xsl, String html) throws Exception
    	{
    		// Création de la source DOM
    		DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
     
    		// creation d'un constructeur
    		DocumentBuilder constructeur = Factory.newDocumentBuilder();
    		File fileXml = new File(xml);
    		Document document = constructeur.parse(fileXml);
            Source source = new DOMSource(document);
     
            // Création du fichier de sortie
            File fileHtml = new File(html);
            Result resultat = new StreamResult(fileHtml);
     
            // Configuration du transformer
            TransformerFactory fabriqueT = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(xsl);
            Transformer transformer = fabriqueT.newTransformer(stylesource);
            transformer.setOutputProperty(OutputKeys.METHOD, "html");
     
            // Transformation
            transformer.transform(source, resultat);
    	}
     
     
    	public static void main(String[] args){
    		try{
    			creerHTML("c:/CreationHTML/Annuaire.xml", "c:/CreationHTML/Annuaire.xsl", "c:/CreationHTML/Annuaire.html");
    		}catch(Exception e){e.printStackTrace();}
    	}
    }

  2. #2
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut
    Ah j'ai oublié de mettre le message d'erreur.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\c:\CreationHTML\Annuaire.html (Syntaxe du nom de fichier, de répertoire ou de volume incorrecte)
    	at org.apache.xalan.transformer.TransformerImpl.createResultContentHandler(TransformerImpl.java:1084)
    	at org.apache.xalan.transformer.TransformerImpl.createResultContentHandler(TransformerImpl.java:975)
    	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1124)
    	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1107)
    	at fichierMRX91.CreationHTML.creerHTML(CreationHTML.java:33)
    	at fichierMRX91.CreationHTML.main(CreationHTML.java:39)
    Caused by: java.io.FileNotFoundException: file:\c:\CreationHTML\Annuaire.html (Syntaxe du nom de fichier, de répertoire ou de volume incorrecte)
    	at java.io.FileOutputStream.open(Native Method)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at org.apache.xalan.transformer.TransformerImpl.createResultContentHandler(TransformerImpl.java:1067)
    	... 5 more
    ---------
    java.io.FileNotFoundException: file:\c:\CreationHTML\Annuaire.html (Syntaxe du nom de fichier, de répertoire ou de volume incorrecte)
    	at java.io.FileOutputStream.open(Native Method)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at org.apache.xalan.transformer.TransformerImpl.createResultContentHandler(TransformerImpl.java:1067)
    	at org.apache.xalan.transformer.TransformerImpl.createResultContentHandler(TransformerImpl.java:975)
    	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1124)
    	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1107)
    	at fichierMRX91.CreationHTML.creerHTML(CreationHTML.java:33)
    	at fichierMRX91.CreationHTML.main(CreationHTML.java:39)
    Je tiens à précisier que mon repertoire CreationHTML contient deux fichiers :
    Annuaire.xml
    et Annuaire.xsl

  3. #3
    Membre expert
    Avatar de ®om
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    2 815
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 2 815
    Points : 3 080
    Points
    3 080
    Par défaut
    met des \\ au lieu des / (t'es sous windows!)

  4. #4
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut
    J'ai fait la modif mais j'ai tjs le meme problème.

  5. #5
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut
    voilà mon fichier annuaire.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <annuaire>
    	<personne id="0">
    		<nom>nom0</nom>
    		<prenom>nom0</prenom>
    		<adresse>adresse0</adresse>
    	</personne>
    	<personne id="1">
    		<nom>nom1</nom>
    		<prenom>nom1</prenom>
    		<adresse>adresse1</adresse>
    	</personne>
    	<personne id="2">
    		<nom>nom2</nom>
    		<prenom>nom2</prenom>
    		<adresse>adresse2</adresse>
    	</personne>
    	<personne id="3">
    		<nom>nom3</nom>
    		<prenom>nom3</prenom>
    		<adresse>adresse3</adresse>
    	</personne>
    	<personne id="4">
    		<nom>nom4</nom>
    		<prenom>nom4</prenom>
    		<adresse>adresse4</adresse>
    	</personne>
    </annuaire>
    et Annuaire.xsl
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" indent="no" encoding="ISO-8859-1"/>
    	<xsl:template match="/">
    		<xsl:apply-templates select="annuaire"/>	
    	</xsl:template>
    	<xsl:template match="annuaire">
    		<html>
    			<head>
    				<title>Annuaire</title>
    			</head>
    			<body>
    				<h1>Annuaire</h1>
    				<xsl:value-of select="count(personne)"/> personnes dans l'annuaire.
    				<table border="1">
    					<tr bgcolor="#C0C0C0">
    						<th>ID</th>
    						<th>Nom</th>
    						<th>Prenom</th>
    						<th>Adresse</th>
    					</tr>
    					<xsl:apply-templates select="personne"/>
    				</table>
    			</body>
    		</html>
    	</xsl:template>
      	<xsl:template match="personne">
      		<tr>
    	  		<td>
    	  			<xsl:value-of select="@id"/>
    	  		</td>
    	  		<td>
    	  			<xsl:value-of select="nom"/>
    	  		</td>
    	  		<td>
    	  			<xsl:value-of select="prenom"/>
    	  		</td>
    	  		<td>
    	  			<xsl:value-of select="adresse"/>
    	  		</td>
    		</tr>
      	</xsl:template>
    </xsl:stylesheet>

  6. #6
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut
    Je pense pas que c'est un problème de "/" ou "\" vu qu'il arrive à lire le fichier annuaire.xml et annuaire.xsl. (pourtant j'utilise c:/CreationHTML/ comme chemin).

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Février 2006
    Messages
    238
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 238
    Points : 267
    Points
    267
    Par défaut
    Salut,

    Essaye de vérifier que le fichier existe : fileHtml.exists()
    et si il n'existe pas fait un fileHtml.createNewFile(), ça m'étonnonerait que le probleme vienne de la mais bon on sais jamais.

    a+

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    57
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 57
    Points : 68
    Points
    68
    Par défaut
    c'est un bug quand on utilise java 1.5

    remplace

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
            File fileHtml = new File(html);
            Result resultat = new StreamResult(fileHtml);
    par


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Result resultat = new StreamResult(html);
    Bonne chance

  9. #9
    Membre du Club Avatar de med_ellouze
    Profil pro
    Étudiant
    Inscrit en
    Mai 2006
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2006
    Messages : 89
    Points : 52
    Points
    52
    Par défaut
    Je te remercie infiniment "XSeb74".
    ça fait quelques jours que je suis bloqué à cause de ça.

  10. #10
    Nouveau membre du Club
    Inscrit en
    Avril 2009
    Messages
    46
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 46
    Points : 32
    Points
    32
    Par défaut
    God, MERCI !!

    J'ai cru devenir fou avec ce probleme!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XSLT 1.0] Transformer un XML en html par XSL
    Par MEH2010DDI dans le forum XSL/XSLT/XPATH
    Réponses: 16
    Dernier message: 11/01/2014, 17h40
  2. Parser du HTML en utilisant Java
    Par zaz147 dans le forum Format d'échange (XML, JSON...)
    Réponses: 7
    Dernier message: 02/09/2008, 12h57
  3. parser un fichier html en utilisant java
    Par taouja dans le forum Services Web
    Réponses: 1
    Dernier message: 16/04/2007, 10h12
  4. [XSLT] Transformer un xml en HTML : problème sur les liens
    Par elhout dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 19/03/2007, 10h46
  5. Transformation xml + xsl -> HTML via PHP
    Par petit-ourson dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 19/10/2003, 22h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo