Insérer les données saisies dans un formulaire dans un fichier XML avec JDOM
bonjour,
j'ai un formulaire html :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <html>
<head>
<title>Document Form </title>
<meta name="DC.Title" xml:lang="EN" content="Dublin Core " />
<meta name="DC.Creator" content="Alan Kelsey" />
<meta name="DC.Subject" xml:lang="EN" content="Dublin Core Meta Tags" />
<meta name="DC.Description" xml:lang="EN" content="documents" />
</head>
<body>
<form action="DocumentAction" method="post" validate="true">
<h2> Formulaire d'ajout de Document</h2>
Titre : <input type="texte" name="titre" size="10"><br>
Creator: <input type="texte" name="creator" size="10"><br>
Subjet : <input type="texte" name="subject" size="10"><br>
Description : <input type="texte" name="description" size="10"><br>
<!-- <input type="file" name="datafile" size="40"><br><br> -->
<input type="submit" value="Enregistrer un document"><br>
</form>
</body>
</html> |
et un fichier xml :
Code:
1 2 3 4
| <?xml version="1.0" encoding="UTF-8"?>
<documents>
<document></document>
</documents> |
et une classe DocumentAction :
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 94 95 96 97 98 99 100 101 102 103 104
| package beanAction;
import java.io.FileOutputStream;
import java.util.Collection;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.input.SAXBuilder;
import com.opensymphony.xwork2.ActionSupport;
public class DocumentAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String titre;
private String creator;
private String subject;
private String description;
static Element racine = new Element("documents");
static Document document = new Document(racine);
public String execute() throws Exception {
Element doc = new Element("document");
racine.addContent(doc);
// Attribute = new Attribute("classe","P2");
// document.setAttribute(get);
doc.setText(getTitre());
doc.setText(getCreator());
doc.setText(getSubject());
doc.setText(getDescription());
enregistre("C:/workspace/GED-1.1/ressources/documents.xml");
return null;
}
static void enregistre(String fichier){
try{
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, new FileOutputStream(fichier));
}catch (java.io.IOException e){}
}
//getters and setters
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} |
et dans mon fichier struts.xml
j'ai l'action suivante :
Code:
1 2 3
| <action name="DocumentAction" class="beanAction.DocumentAction">
<result name="document">Document.html</result>
</action> |
quand je rempli le formulaire , et je clique sur le bouton Enregistrer un document , je regarde le fichier xml , je remarque qu'il ajoute seulement le dernier champ du formulaire dans mon cas (description)
alor moi je veux qu'il ajoute touts les champs (titre, creator, subject, description) dans la balise <document></document>
comme ca :
<document>
<titre> ici le champ saisi dans le formulaire </titre>
<creator> ici le champ saisi dans le formulaire </creator>
<<subject> ici le champ saisi dans le formulaire </subject>
<description> ici le champ saisi dans le formulaire </description>
</document>
et chaque fois que j'ouvre le formulaire et je saisi et je valide il doit m'ajouter un document
j'espère que j'ai bien expliquer mon problem
Merci de votre aide ^^