Bonjours

Je souhaite parser un fichier xml (c'est un exemple pour simplifier le probléme):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<body id="no">
			<style>
				n
			</style>
		</body>
		<body id="yes">
			<style>
				no items founds
			</style>
		</body>
	</head>
</html>
Je veut aller chercher la valeur "no items founds" mais j'arrive pas a spécifier dans mon programme qu'il doit trouver par la balise <body id="yes">.

A chaque fois il va directement dans la première balise body

mon code:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
	   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("new2.xml"));
	      }
	      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
 
 
 
//Ajouter cette méthodes à la classe JDOM2
 
   //On crée une List contenant tous les noeuds "etudiant" de l'Element racine
   Namespace nameSpace = Namespace.getNamespace("html", "http://www.w3.org/1999/xhtml");
   List  head = racine.getChildren("head",nameSpace);
   String resultat="no items founds";
 
   Iterator i = head.iterator();
   while(i.hasNext())
   {
 
      Element courant = (Element)i.next();
      Element body = courant.getChild("body",nameSpace);
 
      if (egal(body.getChild("style",nameSpace).getTextTrim(),resultat))
      { System.out.println(body.getChild("style",nameSpace).getTextTrim());}
      		else
      			System.out.println("no");
   	  }}
 
 
 
	   public static boolean egal (String p1,String p2)
		{boolean p = false;
		int ph1=p1.length();
		int ph2=p2.length();
		if (ph1==ph2)
			{p=true;
		for (int i=0;i<ph1;i++){
			if (p1.charAt(i)!=p2.charAt(i)){
				p=false;}}}
 
		return p;
merci pour votre aide