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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| /*
* OdmSchéma.java
*
* Created on 10 mars 2008, 12:34
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ehr4research2;
import java.io.FileOutputStream;
import org.jdom.*;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.*;
import org.jdom.*;
import org.jdom.output.*;
public class OdmSchéma
{
//Nous allons commencer notre arborescence en créant la racine XML
//qui sera ici "personnes".
static Element racine = new Element("ODM");
//On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
static org.jdom.Document document = new Document(racine);
public static void main(String[] args)
{
//On crée des nouveau elements Elements et on les ajoute en temps qu'Elements de racine
Element etudiant = ajoutElement(racine,"etudiant");
//On crée des nouveaux Attributs et on les ajoute
Attribute classe = ajoutAtribut(etudiant,"classe","P2");
//On crée des nouveaux Elements, on leurs assigne du texte et on les ajoute en temps qu'Elements
Element nom = ajoutElement(etudiant,"nom","testNom");
//Les deux méthodes qui suivent permette d'afficher et d'enregistrer le outPutFile'
affiche();
enregistre("Exercice1.xml");
}
// methode d'ajout d'éléments, retourne un element
public static Element ajoutElement(Element titre, String element)
{ Element elementAjoute = new Element(element);
titre.addContent(elementAjoute);
return elementAjoute;
}
// methode d'ajout d'éléments et de son contenu textuel, retourne un element
public static Element ajoutElement(Element titre, String element, String texte)
{ Element elementAjoute = new Element(element);
elementAjoute.setText(texte);
titre.addContent(elementAjoute);
return elementAjoute;
}
// methode d'ajout d'éléments
public static void ajoutElementVoid(Element titre, String element)
{ Element elementAjoute = new Element(element);
titre.addContent(elementAjoute);
}
// methode d'ajout d'éléments et de son contenu textuel
public static void ajoutElementVoid(Element titre, String element, String texte)
{ Element elementAjoute = new Element(element);
elementAjoute.setText(texte);
titre.addContent(elementAjoute);
}
// methode d'ajout d'atribut, retourne un atribut
public static Attribute ajoutAtribut(Element titre, String element)
{ Attribute atributAjoute = new Attribute(element,"");
titre.setAttribute(atributAjoute);
return atributAjoute;
}
// methode d'ajout d'atribut et de son contenu textuel, retourne un atribut
public static Attribute ajoutAtribut(Element titre, String element,String texte)
{ Attribute atributAjoute = new Attribute(element,texte);
titre.setAttribute(atributAjoute);
return atributAjoute;
}
// methode d'ajout d'atribut
public static void ajoutAtributVoid(Element titre, String element)
{ Attribute atributAjoute = new Attribute(element,"");
titre.setAttribute(atributAjoute);
}
// methode d'ajout d'atribut et de son contenu textuel
public static void ajoutAtributVoid(Element titre, String element, String texte)
{ Attribute atributAjoute = new Attribute(element,texte);
titre.setAttribute(atributAjoute);
}
static void affiche()
{ try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}
static void enregistre(String fichier)
{ try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(document, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}
} |
Partager