i trying read xml file following tag, sax parser unable read nested tags
<active-prod-ownership> <activeprodownership> <product code="3n3" component="tri_score" ordernumber="1-77305469" /> </activeprodownership> </active-prod-ownership>
here code using
public class loginconsumerresponseparser extends defaulthandler { // =========================================================== // fields // =========================================================== static string str="default"; private boolean in_errorcode=false; private boolean in_ack=false; private boolean in_activeprodownership= false; private boolean in_consumerid= false; private boolean in_consumeracctoken=false; public void startdocument() throws saxexception { log.e("i ","in start document"); } public void enddocument() throws saxexception { // nothing log.e("doc read", " ends here"); } /** gets called on opening tags like: * <tag> * can provide attribute(s), when xml like: * <tag attribute="attributevalue">*/ public void startelement(string namespaceuri, string localname, string qname, attributes atts) throws saxexception { if(localname.equals("ack")){ in_ack=true; } if(localname.equals("error-code")){ in_errorcode=true; } if(localname.equals("active-prod-ownership")){ log.e("in", "active product ownership"); in_activeprodownership=true; } if(localname.equals("consumer-id")){ in_consumerid= true; } if(localname.equals("consumer-access-token")) { in_consumeracctoken= true; } } /** gets called on closing tags like: * </tag> */ public void endelement(string namespaceuri, string localname, string qname) throws saxexception { if(localname.equals("ack")){ in_ack=false; } if(localname.equals("error-code")){ in_errorcode=false; } if(localname.equals("active-prod-ownership")){ in_activeprodownership=false; } if(localname.equals("consumer-id")){ in_consumerid= false; } if(localname.equals("consumer-access-token")) { in_consumeracctoken= false; } } /** gets called on following structure: * <tag>characters</tag> */ public void characters(char ch[], int start, int length) { if(in_ack){ str= new string(ch,start,length); } if(str.equalsignorecase("success")){ if(in_consumerid){ } if(in_consumeracctoken){ } if(in_activeprodownership){ str= new string(ch,start,length); log.e("active prod",str); } } } }
but on reaching tag in_activeprodownersip read "<" contents of tag
please need whole data read
the tags in xml file , parser not match. think mixing-up tags attribute names. here code correctly parses sample xml:
public class loginconsumerresponseparser extends defaulthandler { public void startdocument() throws saxexception { system.out.println("startdocument()"); } public void enddocument() throws saxexception { system.out.println("enddocument()"); } public void startelement(string namespaceuri, string localname, string qname, attributes attrs) throws saxexception { if (qname.equals("activeprodownership")) { inactiveprodownership = true; } else if (qname.equals("product")) { if (!inactiveprodownership) { throw new saxexception("product tag not expected here."); } int length = attrs.getlength(); (int i=0; i<length; i++) { string name = attrs.getqname(i); system.out.print(name + ": "); string value = attrs.getvalue(i); system.out.println(value); } } } public void endelement(string namespaceuri, string localname, string qname) throws saxexception { if (localname.equals("activeprodownership")) inactiveprodownership = false; } public void characters(char ch[], int start, int length) { } public static void main(string args[]) throws exception { string xmlfile = args[0]; file file = new file(xmlfile); if (file.exists()) { saxparserfactory factory = saxparserfactory.newinstance(); saxparser parser = factory.newsaxparser(); defaulthandler handler = new test(); parser.parse(xmlfile, handler); } else { system.out.println("file not found!"); } } private boolean inactiveprodownership = false; }
a sample run produce following output:
startdocument() code: 3n3 component: tri_score ordernumber: 1-77305469 enddocument()
Comments
Post a Comment