Bonjour, j'ai créé un fichier xml via JDOM
Maintenant, j'aimerai affiché le contenu de ce fichier :
voici le fichier xml :
<?xml version="1.0" encoding="UTF-8"?>
<tree>
<Repertoire Nom="toto">
<Repertoire Nom="boot">
<File>boot.catalog</File>
<Repertoire Nom="grub">
<File>grub.conf</File>
<File>splash.xpm.gz</File>
<File>stage2_eltorito</File>
</Repertoire>
<File>livecd-initramfs.img</File>
<File>vmlinuz</File>
</Repertoire>
<File>simg</File>
<File>Tototiti</File>
<Repertoire Nom="sysroot" />
</Repertoire>
</tree>
J'utilise la classe java :
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class XMLTreeViewer
{
static org.jdom.Document document;
static Element racine;
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(Path.SAVEXML+"Essai"+Path.SAVEEXT));
}
catch(Exception e){}
//On initialise un nouvel élément racine avec l'élément racine du document.
racine = document.getRootElement();
//Méthode définie dans la partie 3.2. de cet article
afficheALL(racine,"",0);
}
static void afficheALL(Element elt, String tab, int a)
{
//On crée une List contenant tous les noeuds "Repertoire" de l'Element racine
List listRep = elt.getChildren();
//On crée un Iterator sur notre liste
Iterator i = listRep.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 courant = (Element)i.next();
if(courant.getName().equals("Repertoire")){
System.out.println(tab+courant.getAttribute("Nom")+"=======>"+a);
afficheALL(courant,tab+"\t",a+1);
}
//On affiche le nom de l'element courant
System.out.println(tab+courant.getText()+"=======>"+a);
}
}
}
et voila le resultat :
[Attribute: Nom="toto"]=======>0
[Attribute: Nom="boot"]=======>1
boot.catalog=======>2
[Attribute: Nom="grub"]=======>2
grub.conf=======>3
splash.xpm.gz=======>3
stage2_eltorito=======>3
=======>2
livecd-initramfs.img=======>2
vmlinuz=======>2
=======>1
simg=======>1
Tototiti=======>1
[Attribute: Nom="sysroot"]=======>1
=======>1
=======>0
Comme vous l'aurez surement conmpris, la classe lit aussi les balises fermante de Repertoire...
Auriez vous une solution a ce probleme?
D'avance merci
tc
-->Message édité par trollchichon le 03/02/2007 08:39:25<--
-------
Licence Pro GLSI IUT-A Villeneuve d'Ascq
|