Bonjour,
Je voudrais parser un fichier xml avec sax mais je rencontre quelque difficultés.
Tout d'abord voici le fichier xml :
Voila comment j'ai procédé
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <?xml version="1.0" encoding="utf-8"?> <Specialites><specialite> <Id>34</Id> <Nom>Allergologie et immunologie</Nom> </specialite><specialite> <Id>35</Id> <Nom>Anatomie pathologique</Nom> </specialite></Specialites>
Et enfin ma classe parser utilisé ci-dessus qui extends de DefaultHandler
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 public static void main(String[] args) throws ParserConfigurationException, SAXException { // TODO Auto-generated method stub SAXParserFactory fabrique = SAXParserFactory.newInstance(); SAXParser parseur = null; parseur = fabrique.newSAXParser(); //Récuperation xml (string) recupInfoBd recup = new recupInfoBd(); String xml = recup.getFichier(); System.out.println("xmllllllllllll"+xml); //convertion string en inputstream InputStream stream = new ByteArrayInputStream(xml.getBytes()); DefaultHandler gestionnaire = new parser(); try { parseur.parse(stream, gestionnaire); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Comment je peux récupérer ce qu'il y a entre les balises ??
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 public class parser extends DefaultHandler { private Locator locator; static String lastElement; public parser(){ super(); // On definit le locator par defaut. locator = new LocatorImpl(); } @Override public void characters(char[] arg0, int arg1, int arg2) throws SAXException { // TODO Auto-generated method stub } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void endElement(String arg0, String arg1, String arg2) throws SAXException { // TODO Auto-generated method stub } @Override public void endPrefixMapping(String arg0) throws SAXException { // TODO Auto-generated method stub } @Override public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException { // TODO Auto-generated method stub } @Override public void processingInstruction(String arg0, String arg1) throws SAXException { // TODO Auto-generated method stub } @Override public void setDocumentLocator(Locator value) { // TODO Auto-generated method stub locator = value; } @Override public void skippedEntity(String arg0) throws SAXException { // TODO Auto-generated method stub } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("startxml"); } @Override public void startElement(String arg0, String localName, String arg2, Attributes arg3) throws SAXException { // TODO Auto-generated method stub lastElement=localName; System.out.println(lastElement); } @Override public void startPrefixMapping(String arg0, String arg1) throws SAXException { // TODO Auto-generated method stub } }
Je ne pense pas être très loin du but...
Merci de votre aide
Partager