Parsing XML in Java with SAX Parser

YZFRydn

New Member
I have a problem, and it would be very kind if someone could help me out :) I am trying to parse this XML file:\[code\] <data> <day> <match> <team1>foo</team1> <team2>foo</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>10:00</gmt> <groupe>B</groupe> </match> <match> <team1>foo</team1> <team2>foo</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>14:00</gmt> <groupe>A</groupe> </match> </day> <day> <match> <team1>foo</team1> <team2>foo</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>10:00</gmt> <groupe>B</groupe> </match> <match> <team1>foo</team1> <team2>foo</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>14:00</gmt> <groupe>A</groupe> </match> </day></data>\[/code\]But I can't figure out how to accomplish that. With my parser I can parse files like that:\[code\]<data> <match> <team1>aust</team1> <team2>irl</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>10:00</gmt> <groupe>B</groupe> </match> <match> <team1>ind</team1> <team2>afg</team2> <resultfinal></resultfinal> <result1></result1> <result2></result2> <venue>1</venue> <gmt>14:00</gmt> <groupe>A</groupe> </match></data>\[/code\]But I don't know how to store the data for each \[code\]<day>\[/code\] tag.My XML Handler looks like this:\[code\]@Override public void endElement(String uri, String localName, String qName) throws SAXException { if (localName.equalsIgnoreCase("team1")) data.setTeam1(elementValue); else if (localName.equalsIgnoreCase("team2")) data.setTeam2(elementValue); else if (localName.equalsIgnoreCase("resultfinal")) data.setResultfinal(elementValue); else if (localName.equalsIgnoreCase("result1")) data.setResult1(elementValue); else if (localName.equalsIgnoreCase("result2")) data.setResult2(elementValue); else if (localName.equalsIgnoreCase("date")) data.setDate(elementValue); else if (localName.equalsIgnoreCase("venue")) data.setVenue(elementValue); else if (localName.equalsIgnoreCase("gmt")) data.setGmt(elementValue); } @Override public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException { // TODO Auto-generated method stub super.startElement(uri, localName, qName, attributes); elementOn = true; if (localName.equals("data")) { data = http://stackoverflow.com/questions/12448570/new XMLGettersSetters(); } else if (localName.equals("match")) { } }\[/code\]But with that I can just only parse files like the second. How can I parse the first file and how should I store the data? I can't store it in an Arraylist. I am glad for every help. Thank you.
 
Back
Top