I am trying to parse a moderately large xml file, store it in an ArrayList then display results in an listview. Currently I have got a problem, but cannot find a solution. Firstly here is my sax parser code:
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean DayofWeek = false;
boolean DateDD = false;
String dayofweek = null;
String datedd = null;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("DayofWeek")) {
DayofWeek = true;
}
else if (qName.equalsIgnoreCase("Date-DD")) {
DateDD = true;
}
// ommited
}
public void endElement(String uri, String localName,String qName) throws SAXException {
}
public void characters(char ch[], int start, int length) throws SAXException {
if (DayofWeek) {
System.out.println(new String(ch,start,length));
dayofweek = new String(ch, start, length);
DayofWeek = false;
}
else if (DateDD) {
System.out.println(new String(ch,start,length));
datedd = new String(ch, start, length);
DateDD = false;
}
// ommited
else if (dayofweek != null) {
System.out.println("Do i reach here?");
addElement(dayofweek, datedd, datemm, dateyy, time, competition, home, away, venue); //is run
}
}
};
saxParser.parse(is, handler);
} catch (Exception e) {
System.out.println("Sax Error");
Log.w("SaxError", e);
}
}
private void addElement(String dayofweek, String datedd, String datemm, String dateyy, String time, String competition, String home, String away, String venue) {
System.out.println("Add Element");
AddObjectToList(dayofweek, datedd);
}
// Add one item into the Array List
public void AddObjectToList(String dayofweek, String datedd, String datemm, String dateyy, String time, String competition, String home, String away, String venue) {
System.out.println("AddObjectToList");
fixture = new FixtureSupport();
fixture.setDayOfWeek(dayofweek);
fixture.setDateDD(datedd);
itemList.add(fixture);
}
In logcat, this will print all the information correctly, but nothing will go into the AddElement document so nothing appears in the listview. Whereas, if I were to change all the ‘else if’ statements to ‘if’ statements the code will print out in the listview but each XML element will get repeated about 10 or so times (seems random). Whilst it also repeats itself it only prints one of the xml elements at a time and gradually other elements get added to it.
I can’t understand why this is happening, any help please guys!
Thanks
Fix this issue:
1) It’s completely confusing
2) variables should always start with a lower case letter
3) I have no clue if this affects your code logic
Try:
Ref: Java Naming Conventions
Your characters() method needs to append to a StringBuffer, not all characters may be sent at once, so you have to keep appending then in the endElement() method you call .toString on your buffer.
Ref: OTT XML Parsing
Methods should also start with a lowercase letter just for clarity (Class names are uppercase) change:
to
and see how we go from there.