XML SAX Parser funktioniert nicht

  • Antworten:2
ZaRa
  • Forum-Beiträge: 2

17.09.2012, 21:19:28 via Website

Hallo!
Ich probiere schon den ganzen Tag meinen XML Parser zum laufen zu kriegen, doch hatte bisher keinen Erfolg, da dachte ich mir, ich frage euch einfach. :)

Also ich möchte dieses Dokument hier parsen:

1<data>
2 <day>
3
4 <match>
5
6 <team1>ind</team1>
7 <team2>afg</team2>
8
9 <resultfinal></resultfinal>
10 <result1></result1>
11 <result2></result2>
12 <date>19.09</date>
13 <venue>1</venue>
14 <gmt>14:00</gmt>
15
16
17 </match>
18
19 <match>
20
21 <team1>eng</team1>
22 <team2>afg</team2>
23
24 <resultfinal></resultfinal>
25 <result1></result1>
26 <result2></result2>
27
28 <date>21.09</date>
29 <venue>1</venue>
30 <gmt>14:00</gmt>
31
32
33 </match>
34 <match>
35
36 <team1>ind</team1>
37 <team2>eng</team2>
38
39 <resultfinal></resultfinal>
40 <result1></result1>
41 <result2></result2>
42 <date>23.09</date>
43 <venue>2</venue>
44 <gmt>10:00</gmt>
45
46
47 </match>
48 </day>
49 <day>
50 <match>
51
52 <team1>aust</team1>
53 <team2>irl</team2>
54
55 <resultfinal></resultfinal>
56 <result1></result1>
57 <result2></result2>
58 <date>19.08</date>
59 <venue>1</venue>
60 <gmt>10:00</gmt>
61
62
63 </match>
64 <match>
65
66 <team1>aust</team1>
67 <team2>west</team2>
68
69 <resultfinal></resultfinal>
70 <result1></result1>
71 <result2></result2>
72 <date>22.09</date>
73 <venue>1</venue>
74 <gmt>14:00</gmt>
75
76
77 </match>
78 <match>
79
80 <team1>west</team1>
81 <team2>irl</team2>
82
83 <resultfinal></resultfinal>
84 <result1></result1>
85 <result2></result2>
86
87 <date>24.09</date>
88 <venue>1</venue>
89 <gmt>14:00</gmt>
90
91
92 </match>
93
94 </day>
95
96 </data>
Um sich besser vorzustellen, wie die Struktur des Dokumentes aussieht:
<data>
<day>
<match> [...]
</match>
</day>

<day>
<match> [...]
</match>
</day>
</data>

und mein Parser sieht so aus:

MainActivity:

1public class MainActivity extends Activity {
2 XMLHandler data;
3
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.activity_main);
8
9
10 try {
11 Log.i("PARSER", "versuch!");
12
13 /**
14 * Create a new instance of the SAX parser
15 **/
16
17 SAXParserFactory saxPF = SAXParserFactory.newInstance();
18 SAXParser saxP = saxPF.newSAXParser();
19 XMLReader xmlR = saxP.getXMLReader();
20
21
22 URL url = new URL("my xml file"); // URL of the XML
23
24 /**
25 * Create the Handler to handle each of the XML tags.
26 **/
27 data = new XMLHandler();
28 xmlR.setContentHandler(data);
29 xmlR.parse(new InputSource(url.openStream()));
30
31
32 } catch (Exception e) {
33 System.out.println(e);
34 Log.i("PARSER", "ES KLAPPT NICHT!");
35 }
36
37
38
39
40 TextView txt1 = (TextView) findViewById(R.id.txtview1);
41 txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString()); // Ab hier crasht die app. Da ich keinen Wert zurückkriege(NullPointerException)
42
43 }

XMLHandler:

1public class XMLHandler extends DefaultHandler {
2
3 private Tournament tournament;
4 private TournamentDay currentDay;
5 private Match currentMatch;
6 private StringBuilder builder;
7
8 @Override
9 public void endElement(String uri, String localName, String qName)
10 throws SAXException {
11
12 if (qName.equalsIgnoreCase("team1"))
13 currentMatch.setTeam1(builder.toString());
14 else if (qName.equalsIgnoreCase("team2"))
15 currentMatch.setTeam2(builder.toString());
16 else if (qName.equalsIgnoreCase("resultfinal"))
17 currentMatch.setResultfinal(builder.toString());
18 else if (qName.equalsIgnoreCase("result1"))
19 currentMatch.setResult1(builder.toString());
20 else if (qName.equalsIgnoreCase("result2"))
21 currentMatch.setResult2(builder.toString());
22 else if (qName.equalsIgnoreCase("venue"))
23 currentMatch.setVenue(builder.toString());
24 else if (qName.equalsIgnoreCase("gmt"))
25 currentMatch.setGmt(builder.toString());
26 else if (qName.equals("match"))
27 currentDay.add(currentMatch); // neues Match(Spiel)
28 else if (qName.equals("day"))
29 tournament.add(currentDay); // neuer Tag
30 }
31
32 @Override
33 public void startElement(String uri, String localName, String qName,
34 org.xml.sax.Attributes attributes) throws SAXException {
35 if (qName.equals("data")) {
36 tournament = new Tournament();
37 }
38 if (qName.equals("day")) {
39 currentDay = new TournamentDay();
40 }
41 else if (qName.equals("match")) {
42 currentMatch = new Match();
43 }
44 else {
45 builder = new StringBuilder();
46 }
47 }
48
49 @Override
50 public void characters(char[] chars, int start, int length) throws SAXException {
51 builder.append(chars, start, length);
52 }
53
54 public Tournament getTournament() {
55 return tournament;
56 }
57 }

Tournament:

1public class Tournament {
2
3 private List<TournamentDay> days;
4
5 public Tournament() {
6 this.days = new ArrayList<TournamentDay>();
7 }
8
9 public void add(TournamentDay day) {
10 days.add(day);
11 }
12 public TournamentDay get(int i) {
13 return days.get(i);
14 }
15 }

Match:

1public class Match {
2 private String team1;
3 private String team2;
4 private String resultfinal;
5 private String result1;
6 private String result2;
7 private String date;
8 private String venue;
9 private String gmt;
10
11 public String getTeam1() {
12 return team1;
13 }
14
15 public void setTeam1(String team1) {
16 this.team1 = team1;
17 }
18
19 public String getTeam2() {
20 return team2;
21 }
22
23 public void setTeam2(String team2) {
24 this.team2 = team2;
25 }
26
27 public String getResultfinal() {
28 return resultfinal;
29 }
30
31 public void setResultfinal(String resultfinal) {
32 this.resultfinal = resultfinal;
33 }
34
35 public String getResult1() {
36 return result1;
37 }
38
39 public void setResult1(String result1) {
40 this.result1 = result1;
41 }
42
43 public String getResult2() {
44 return result2;
45 }
46
47 public void setResult2(String result2) {
48 this.result2 = result2;
49 }
50
51 public String getDate() {
52 return date;
53 }
54
55 public void setDate(String date) {
56 this.date = date;
57 }
58
59 public String getVenue() {
60 return venue;
61 }
62
63 public void setVenue(String venue) {
64 this.venue = venue;
65 }
66
67 public String getGmt() {
68 return gmt;
69 }
70
71 public void setGmt(String gmt) {
72 this.gmt = gmt;
73 }
74
75 }


Aber ich bekomme immer einen Fehler. Hier mein LogCat:


109-17 15:26:49.299: I/System.out(3392): java.lang.NullPointerException
2 09-17 15:26:49.299: I/PARSER(3392): ES KLAPPT NICHT!
3 09-17 15:26:49.299: D/AndroidRuntime(3392): Shutting down VM
4 09-17 15:26:49.299: W/dalvikvm(3392): threadid=1: thread exiting with uncaught exception (group=0xb40a64f0)
5 09-17 15:26:49.299: I/Process(3392): Sending signal. PID: 3392 SIG: 9
6 09-17 15:26:49.299: D/AndroidRuntime(3392): procName from cmdline: com.example.xmldownloader
7 09-17 15:26:49.299: E/AndroidRuntime(3392): in writeCrashedAppName, pkgName :com.example.xmldownloader
8 09-17 15:26:49.299: D/AndroidRuntime(3392): file written successfully with content: com.example.xmldownloader StringBuffer : ;com.example.xmldownloader
9 09-17 15:26:49.299: E/AndroidRuntime(3392): FATAL EXCEPTION: main
10 09-17 15:26:49.299: E/AndroidRuntime(3392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmldownloader/com.example.xmldownloader.MainActivity}: java.lang.NullPointerException
11 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
12 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1726)
13 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
14 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:949)
15 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.os.Handler.dispatchMessage(Handler.java:99)
16 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.os.Looper.loop(Looper.java:130)
17 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.main(ActivityThread.java:3770)
18 09-17 15:26:49.299: E/AndroidRuntime(3392): at java.lang.reflect.Method.invokeNative(Native Method)
19 09-17 15:26:49.299: E/AndroidRuntime(3392): at java.lang.reflect.Method.invoke(Method.java:507)
20 09-17 15:26:49.299: E/AndroidRuntime(3392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
21 09-17 15:26:49.299: E/AndroidRuntime(3392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
22 09-17 15:26:49.299: E/AndroidRuntime(3392): at dalvik.system.NativeStart.main(Native Method)
23 09-17 15:26:49.299: E/AndroidRuntime(3392): Caused by: java.lang.NullPointerException
24 09-17 15:26:49.299: E/AndroidRuntime(3392): at com.example.xmldownloader.MainActivity.onCreate(MainActivity.java:58)
25 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
26 09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662)
27 09-17 15:26:49.299: E/AndroidRuntime(3392): ... 11 more

Also es hadelt sich um ein Tunier, und da das XML dokument sehr flexibel sein muss, habe ich in jedem <day> verschieden viele <match>[es].
Kann mir jemand vielleicht weiterhelfen?


Vielen Dank! :)
Gruß RaZa

— geändert am 17.09.2012, 21:37:15

Antworten
Christian
  • Forum-Beiträge: 307

18.09.2012, 18:38:20 via Website

Hi ZaRa,

dein Problem ist das du einen Fehler in deinem Parser hast.
in der LogCat steht:
09-17 15:26:49.299: I/PARSER(3392): ES KLAPPT NICHT!
soweit ich das sehe wird das nur ausgeben wenn beim Parser eine Exception geworfen wird.

Um dir die eigentliche Fehlerquelle anzeigen zu lassen solltest du den Stracktrace oder der Mesage ausgeben und nicht nur "ES KLAPPT NICHT".
(kleiner Tipp: e.printStackTrace(); oder e.getMessage())

Mfg Christian

Antworten