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
| package beanAction;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import com.opensymphony.xwork2.ActionSupport;
public class AfficherDocAction 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 {
// On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try {
// On crée un nouveau document JDOM a partir du fichier xml de nom
// exemple
// Le parsing est terminé ;)
document = sxb
.build(new File(
"C:/Documents and Settings/Administrateur/Mes documents/workspace/GED-1.1/ressources/documents.xml"));
}
// Exception si le fichier est mal formé
catch (JDOMException e1) {
System.err.println("Fichier xml mal formé");
}
// Exception si le chemin du fichier n'est pas correcte
catch (IOException e2) {
System.err.println("Erreur IO...");
}
// On initialise un nouveau élément racine avec l'élément racine du
// document.
racine = document.getRootElement();
afficheALL();
return SUCCESS;
}
private static void afficheALL() {
// On crée une List contenant tous les noeuds "etudiant" de l'Element
// racine
List listDoc = racine.getChildren("document");
// On crée un Iterator sur notre liste
Iterator i = listDoc.iterator();
while (i.hasNext()) {
// On recrée l'Element courant à chaque tour de boucle afin de
// pouvoir utiliser les méthodes propres aux Element comme :
// selectionner un noeud fils, modifier du texte, etc...
Element elementcourant = (Element) i.next();
System.out.println("Titre : "
+ elementcourant.getChildText("titre"));
System.out.println("Createur : "
+ elementcourant.getChildText("creator"));
System.out.println("Sujet : "
+ elementcourant.getChildText("subject"));
System.out.println("Description : "
+ elementcourant.getChildText("description"));
}
}
// 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;
}
} |
Partager