Bon, là je ne suis pas malin, mais bon.
J'ai besoin de vous pour éclaircir mes idées.
J'ai définit un formaliste pour mon document 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE publipostage PUBLIC "-//" "publipostage.dtd">
 
 
<publipostage>
 
 <fields id="user">
  <field id="nom">Nom du collaborateur</field>
  <field id="prenom">Prénom du collaborateur</field>
 </fields>
 
 <user>
  <fields>
   <field id="nom">GOUBE</field>
   <field id="prenom">Olivier</field>
  </fields>
 </user>
 
 <fields id="customer">
  <field name="nom">Nom du contact</field>
  <field name="prenom">Prénom du contact</field>
  <field name="adresse">Adresse du contact</field>
 </fields>
 
 <customer>
  <fields>
   <field name="nom">COLLARD</field>
   <field name="prenom">Alexandre</field>
   <field name="adresse">Rue du mec bourré</field>
  </fields>
 </customer> 
 
 <customer>
  <fields>
   <field name="nom">HUMEAU</field>
   <field name="prenom">Xavier</field>
   <field name="adresse">Rue du mec encore plus bourré</field>
  </fields>
 </customer>
 
 
</publipostage>
J'aimerai maintenant pouvoir le créer en Java.
Alors j'ai commencé avec çà :

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
 
try{
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.newDocument();         
         Element rootElement = doc.createElement("publipostage");
         Node node = doc.createElement("fields");
         rootElement.appendChild(node);
         doc.appendChild(rootElement);
 
 
         XMLUtils.writeDocument(doc, System.out);
 
 
         } catch(Exception ex){
            ex.printStackTrace();
         }

Et voici le writeDocument :
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
 
public static boolean writeDocument(Document doc,OutputStream out){
     try{
        // Use a Transformer for output
        TransformerFactory tFactory =
           TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
 
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result); 
        return true;
     } catch(Exception ex){
        ex.printStackTrace();
        return false;
     }
  }
Et voici le résultat :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
<?xml version="1.0" encoding="UTF-8"?><publipostage><fields/></publipostage>
Donc avec çà, j'ai un bon résultat déjà ! Mais j'aimerai changer l'attribut "encoding" de ma première ligne car avec ma solution actuellement, il me mets UTF-8.
Ensuite j'aimerai ajouter la ligne correspondant à l'appel de ma DTD qui est pour moi la suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
<!ELEMENT publipostage (fields+,user,customer*)>
 
<!ELEMENT fields (field+)>
<!ATTLIST fields  id              CDATA              #IMPLIED>
 
<!ELEMENT field (#PCDATA)>
<!ATTLIST fields  id              CDATA              #IMPLIED>
<!ATTLIST fields  name            CDATA              #IMPLIED>
 
<!ELEMENT user (fields)>
 
<!ELEMENT customer (fields)>
Alors si quelqu'un peut m'aider pour toutes ces petites choses ....
Ce serait sympa.
D'avance merci .