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
|
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
public class NewClass
{
static org.jdom.Document document;
static Element racine;
static void afficheALL()
{
//On crée une List contenant tous les noeuds "etudiant" de l'Element racine
/* List listLocation = racine.getChildren("LOCATION_PROPERTIES");
//On crée un Iterator sur notre liste
Iterator i = listLocation.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 :
//sélectionner un nud fils, modifier du texte, etc...
Element segment = (Element)i.next();
//On affiche le nom de lélément courant
System.out.println(segment.getChild("SEGMENT_PROPERTIES").getText());
}
*/
for(Element element : racine.getChildren("LOCATION_PROPERTIES"))
{
int j=0;
Element segment;
while( (segment = element.getChild("Segment"+j))!=null )
{
System.out.println(segment.getText());
j++;
}
}
}
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("C:\\Users\\Thomas\\Desktop\\Chalmers\\routes.xml"));
}
catch(JDOMException | IOException e){}
//On initialise un nouvel élément racine avec l'élément racine du document.
racine = document.getRootElement();
afficheALL();
}
} |
Partager