java - Passing Attributes from startElement to EndElement in SAX -
we trying parse xml using sax parser. our environment: java version: 1.7
<wrappercell borderwidth="0.9f" border="no_border" colspan="1"> <phrase font="bold_arial"> <token>1234</token> </phrase> </wrappercell>
in our startelement doing below
public void startelement(string uri, string localname, string qname, attributes attributes){ if("wrappercell".equals(qname)){ elemenstack.push(attributes); }else if("phrase".equals(qname)){ elemenstack.push(attributes); } }
in our endelement wanted refer attributes pushed during startelement
public void endelement(string uri, string localname, string qname) throws saxexception { if("wrappercell".equals(qname)){ system.out.println(((attributes)elemenstack.pop()).getlength()); }else if("phrase".equals(qname)){ system.out.println(((attributes)elemenstack.pop()).getlength()); } }
this returns 0 getlength(). refer other ques says attributes objects has same instances @ each startelement.
is there option other below tried such can refer startelement values in endelement;
our plan solution
public void startelement(string uri, string localname, string qname, attributes attributes){ if("wrappercell".equals(qname)){ elementattribute.put(attribute.getqname(1),attributes.getvalue(1)); elemenstack.push(elementattribute); }else if("phrase".equals(qname)){ elementattribute.put(attribute.getqname(1),attributes.getvalue(1)); elemenstack.push(elementattribute); } } public void endelement(string uri, string localname, string qname) throws saxexception { if("wrappercell".equals(qname)){ system.out.println(((hashmap<string,string>)elemenstack.pop()).size()); }else if("phrase".equals(qname)){ system.out.println(((hashmap<string,string>)elemenstack.pop()).size()); } }
not direct answer question, using stax instead of sax easier issues that.
sax push-parser, meaning pushes events executing callback method.
stax pull-parser, meaning request next event stream. allows use call stack in code control "context".
both shipped java 7.
Comments
Post a Comment