Bonjour a tous je dois réaliser le parsing d'un fichier xml d'une petite application en vue de l'utiliser plus tard dans mon projet.
Mon projet va consister a recuperer des documents xml d'un coollegue et de les parser pour recuperer des données du genre route intersection stop.... pour ensuite les transmettre a un collegue qui va dessiner la carte en question.
dans la tache confié on doit recuperer une route et 2 intersections voila 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 * carteHandler.java
 *
 * Created on 27 janvier 2005, 16:40
 */
 
package tacheparseur;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*; 
import java.io.*; 
import java.util.*; 
/**
 *
 * @author  royal
 */
 
public class CarteHandler extends DefaultHandler{
   //résultats de notre parsing
   private List<carte> projet;
   private carte carte;
   //flags nous indiquant la position du parseur
   private boolean inProjet, inCarte, inRoute, inIntersection;
 
   // simple constructeur
   public CarteHandler(){
      super();	
 
   }
   //détection d'ouverture de balise
   public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes attributes)
                  throws SAXException{
      System.out.println(uri+" "+localName+" "+qName);
      if(qName.equals("projet")){
         projet = new LinkedList<carte>();
         inProjet = true;
      }else if(qName.equals("carte")){
         carte = new carte();
         try{
         	int id = Integer.parseInt(attributes.getValue("id"));
            carte.setId(id);
         }catch(Exception e){
         	//erreur, le contenu de id n'est pas un entier
            throw new SAXException(e);
         }
         inCarte = true;	
      }else if(qName.equals("route")){
         inRoute = true;	
      }else if(qName.equals("intersection")){
         inIntersection = true;	
      }else{
         //erreur, on peut lever une exception
         throw new SAXException("Balise "+qName+" inconnue.");	
      }
   }
   //détection fin de balise
   public void endElement(String uri,
                       String localName,
                       String qName)
                throws SAXException{
      if(qName.equals("projet")){
         inProjet = false;
      }else if(qName.equals("carte")){
      	 projet.add(carte);
      	 carte = null;
         inCarte = false;	
      }else if(qName.equals("route")){
         inRoute = false;	
      }else if(qName.equals("intersection")){
         inIntersection = false;		
      }else{
         //erreur, on peut lever une exception
         throw new SAXException("Balise "+qName+" inconnue.");	
      }          	
   }
   //détection de caractères
   public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException{
      String lecture = new String(ch,start,length);
      if(inRoute){
        carte.setRoute(lecture);	
      }else if(inIntersection){
         carte.setIntersection(lecture);	
      }      	
   }
   //début du parsing
   public void startDocument() throws SAXException {
   	  System.out.println("Début du parsing");
   }
   //fin du parsing
   public void endDocument() throws SAXException {
   	  System.out.println("Fin du parsing");
   	  System.out.println("Resultats du parsing");
   	  for(carte p : projet){
   	     System.out.println(p);
   	  }
   }
 
   // test
   public static void main(String[] args){
      try{
         // création d'une fabrique de parseurs SAX
         SAXParserFactory fabrique = SAXParserFactory.newInstance();
 
         // création d'un parseur SAX
         SAXParser parseur = fabrique.newSAXParser();
 
         // lecture d'un fichier XML avec un DefaultHandler
         File fichier = new File("./src/tacheparseur/carte.xml");
         DefaultHandler gestionnaire = new CarteHandler();
         parseur.parse(fichier, gestionnaire);
 
      }catch(ParserConfigurationException pce){
         System.out.println("Erreur de configuration du parseur");
         System.out.println("Lors de l'appel à SAXParser.newSAXParser()");
      }catch(SAXException se){
         System.out.println("Erreur de parsing");
         System.out.println("Lors de l'appel à parse()");
         se.printStackTrace();
      }catch(IOException ioe){
         System.out.println("Erreur d'entrée/sortie");
         System.out.println("Lors de l'appel à parse()");
      }
   }	
}
avec sa classe:
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
package tacheparseur;
 
/**
 *
 * @author  royal
 */
public class carte {
     private int id;
     private String route,intersection;
    /** Creates a new instance of carte */
    public carte() {
 
    }
     public int getId(){
       return id;
   }
   public String getRoute(){
       return route;
   }
   public String getIntersection(){
       return intersection;
   }
    public void setId(int id){
       this.id = id;
   }
    public void setRoute(String route){
       this.route = route;
   }
   public void setIntersection(String intersection){
       this.intersection = intersection;
   }
   public String toString(){
      return new StringBuffer("Route : ").append(route).append(", ")
      .append("Intersection : ").append(intersection) .toString();
   }
}
et son fichier xml:
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
<?xml version="1.0" encoding="UTF-8"?>
 
<!--
    Document   : carte.xml
    Created on : 27 janvier 2005, 15:11
    Author     : royal
    Description:
        Purpose of the document follows.
-->
 
<projet>
<carte id="0">
        <route>N195 </route> 
        <intersection>x:33,y:25</intersection>
        <intersection>x:63,y:33</intersection>
 
</carte>
</projet>
le probleme et que je recupere la route mais qu'une seule intersection et je sais pas pourkoi.

resultat:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
init:
deps-jar:
compile-single:
run-single:
Début du parsing
  projet
  carte
  route
  intersection
  intersection
Fin du parsing
Resultats du parsing
Route : N195 , Intersection : x:63,y:33
BUILD SUCCESSFUL (total time: 0 seconds)