java - Access a node in a xml using Sax parsing -
am trying parse xml file ..and trying read employee nodes...
<?xml version="1.0" encoding="utf-8"?> <employees> <employee id="111"> <firstname>rakesh</firstname> <lastname>mishra</lastname> <location>bangalore</location> <secretary> <employee id="211"> <firstname>andy</firstname> <lastname>mishra</lastname> <location>bangalore</location> </employee> </secretary> </employee> <employee id="112"> <firstname>john</firstname> <lastname>davis</lastname> <location>chennai</location> </employee> <employee id="113"> <firstname>rajesh</firstname> <lastname>sharma</lastname> <location>pune</location> </employee> </employees>
and in handler ..i have below...
class saxhandler extends defaulthandler{ @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { if(qname.equals("employee")){ system.out.print("its employee ...and not secretary"); /*for(int i=0;i< attributes.getlength();i++){ system.out.print("attr " +attributes.getqname(i)+ " value " +attributes.getvalue(i)); }*/ system.out.println(); } }
how can know if the employee secretary or not
regards
you need add if
inside startelement
detect secretary
start element event, , set flag can test when inside employee
tag. reset flag when leave secretary
element. example
class saxhandler extends defaulthandler { private boolean insidesecretarytag = false; @override public void startelement(...) throws saxexception { if(qname.equals("employee")){ if(insidesecretarytag) { // secretary } else { // not secretary } } if(qname.equals("secretary")){ insidesecretarytag = true; } public void endelement(...) { if(qname.equals("secretary")){ insidesecretarytag = false; } } }
Comments
Post a Comment