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
|
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
public class Essaye2
{
static org.jdom.Document document;
static Element racine;
public static void afficheALL()
{
//On crée une List contenant tous les noeuds "etudiant" de l'Element racine
List listEtudiants = racine.getChildren("etudiant" );
//On crée un Iterator sur notre liste
Iterator i = listEtudiants.iterator();
Element courant = (Element)i.next();
//On affiche le nom de l'element courant
System.out.println(courant.getChild("nom" ).getText());
System.out.println(courant.getChild("prenom" ).getText());
}
public static void main(String[] args)
{
//On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try
{
//On crée un nouveau document JDOM avec en argument le fichier XML
//Le parsing est terminé ;)
document = sxb.build(new File(MonFichier())); //normalement c MonFichier.xml
}
catch(Exception e){}
//On initialise un nouvel élément racine avec l'élément racine du document.
racine = document.getRootElement();
afficheALL();
}
public static String MonFichier()
{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<personnes>"+
"<etudiant >"+
"<nom>MonNom</nom>"+
"<prenom>MonPrenom</prenom>"+
"</etudiant>"+
"</personnes>";
}
} |
Partager