java - How to store Data after SAX parsing -
i use sax parser parse xml file , store data arraylists-hashmaps within enddocument method of parser. seems though after parsing ends references gone , when try access them main method 0 arraylist-hashmap size(). in other words save data after parsing can print them out during sax-parsing. how possible save them in order re-use them after parsing done? have tried creating list of objects containing information need didnt work.
handler code:
public class myhandler extends defaulthandler { private list<string> rules = new arraylist<string>(); public myhandler() {acc = new stringbuilder();} public void startdocument(){ string startdocumentstring="parsing started ----- discovered rules :"; system.out.println(startdocumentstring);} public void enddocument(){ string enddocumentstring="\n \nparsing ended ------"; system.out.println(enddocumentstring); for(int i=0 ; i< rulesnumber ; i++){ setrules(result[(0+(3*i))]); system.out.println("assosiative rule " + (i+1) +": "+ result[(0+(3*i))]);} public void startelement(string namespaceuri, string localname, string qname, attributes atts) { if(qname.equals("associationrule")) { setrulesnumber();} public void endelement(string namespaceuri,string localname, string qname) { (acc.tostring()).trim(); system.out.print(acc.tostring()); results.add(acc.tostring()); acc.setlength(0); unsetdecision(); } public void characters (char[] ch, int start , int length) { if(decision) acc.append(ch, start, length); } public void setrules(string s){rules.add(s);} public list<string> getrules(){return rules;}
you need keep reference contenthandler
e.g.
//keep reference myhandler myhandler handler = new myhandler(); xmlreader parser = xmlreaderfactory.createxmlreader(); parser.setcontenthandler(handler); parser.parse(mysource); //get results list<string> rules = handler.getrules();
Comments
Post a Comment