Bonjour à tous,
ça fait bientôt deux jours que je n'arrive pas à lire un fichier XML, le logcat m'indique toujours que je ne soulève pas une exception et donc qu'il ne peut pas continuer.
J'aimerais comprendre pourquoi...
Voilà ma première classe héritant 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
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 package com.example.toolbook; import java.util.ArrayList; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ParserXMLHandler extends DefaultHandler { private final String MOT_FRANCAIS = "motFrancais"; private final String MOT_ANGLAIS = "motAnglais"; private final String DEF_FRANCAIS = "definitionFrancais"; private final String DEF_ANGLAIS = "definitionAnglais"; private final String TEST1 = "test1"; private final String TEST2 = "test2"; private final String TEST3 = "test3"; private final String TEST4 = "test4"; private final String TEST5 = "test5"; private ArrayList<String> entries; private Entry currentFeed; private StringBuffer buffer; public void processingInstruction(String target, String data) { try { super.processingInstruction(target, data); } catch (SAXException e) { e.printStackTrace(); } } public ParserXMLHandler(){super();} public void startDocument() throws SAXException { super.startDocument(); entries = new ArrayList<String>(); } public void startElement(String uri, String localName, String name) { if(localName.equalsIgnoreCase(TEST1)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST2)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST3)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST4)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST5)) this.currentFeed.setTest(buffer.toString()); } public void endElement(String uri, String localName, String name) { if(localName.equalsIgnoreCase(MOT_FRANCAIS)) { this.currentFeed.setMotFrancais(buffer.toString()); buffer = null; } if(localName.equalsIgnoreCase(MOT_ANGLAIS)) { this.currentFeed.setMotAnglais(buffer.toString()); buffer = null; } if(localName.equalsIgnoreCase(DEF_FRANCAIS)) { this.currentFeed.setDefFrancais(buffer.toString()); buffer = null; } if(localName.equalsIgnoreCase(DEF_ANGLAIS)) { this.currentFeed.setDefAnglais(buffer.toString()); buffer = null; } if(localName.equalsIgnoreCase(TEST1)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST2)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST3)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST4)) this.currentFeed.setTest(buffer.toString()); if(localName.equalsIgnoreCase(TEST5)) this.currentFeed.setTest(buffer.toString()); } public void characters(char[] ch, int start, int length) { String lecture = new String(ch, start, length); if(buffer != null) buffer.append(lecture); } public ArrayList<String> getData() { return entries; } }
Voilà maintenant ma classe qui me permet de récupérer les données :
et enfin le logcat :
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 package com.example.toolbook; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.content.Context; import android.content.res.AssetManager; public class ContainerData { static public Context context; public ContainerData(){} public static ArrayList<String> getFeeds() { SAXParserFactory fabrique = SAXParserFactory.newInstance(); SAXParser parseur = null; ArrayList<String> entries = null; try { parseur = fabrique.newSAXParser(); } catch(ParserConfigurationException e){e.printStackTrace();} catch(SAXException e){e.printStackTrace();} AssetManager asset = context.getAssets(); InputStream inputStream = null; try { inputStream = asset.open("dico.xml"); } catch (IOException e2) { e2.printStackTrace(); } InputSource input = null; input = new InputSource(inputStream); DefaultHandler handler = new ParserXMLHandler(); try { parseur.parse(input, handler); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } entries = ((ParserXMLHandler) handler).getData(); return entries; } }
Si vous pouviez m'aider, ce serait vraiment très gentil !![]()
Partager