Parsing XML and get required data from multiple tags with the same name using SAX parser in java -


this question has answer here:

<report xmlns="http://developer.cognos.com/schemas/report/7.0/" expressionlocale="en-us"> <modelpath>/content/package[@name=’go sales , retailers’] /model[@name=’model’]</modelpath> <queries> <query name="query1"> <source> <model/> </source> <selection> <dataitem name="revenue" aggregate="total"> <expression>[gosales_goretailers].[orders].[revenue]</expression> </dataitem> <dataitem name="sales territory" aggregate="none"> <expression>[gosales_goretailers].[countries].[sales territory] </expression> </dataitem> <dataitem name="order method" aggregate="none"> <expression>[gosales_goretailers].[orders].[order method]</expression> </dataitem> <dataitem name="order year" aggregate="none"> <expression>[gosales_goretailers].[orders].[order year]</expression> </dataitem> <dataitem name="product line" aggregate="none"> <expression>[gosales_goretailers].[products].[product line]</expression> </dataitem> 

i want store text value within expression tag corresponding data item value using sax parser.

means want output this: (1) revenue=[gosales_goretailers].[orders].[revenue] (2) sales territory=[gosales_goretailers].[countries].[sales territory]...

i used following code store text values in list within expression tag

       final arraylist<string> dataitemlist=new arraylist<string>();         final arraylist<string> expressionlist=new arraylist<string>();         final arraylist<string> retlist=new arraylist<string>();          try         {         saxparserfactory factory = saxparserfactory.newinstance();         saxparser saxparser = factory.newsaxparser();           defaulthandler handler = new defaulthandler() {              string retdata="";             boolean bdataitem = false;             boolean bexpression=false;                public void startelement(string uri, string localname,string qname,                          attributes attributes) throws saxexception {                    if (qname.equalsignorecase("dataitem")) {                     bdataitem = true;                      string name=attributes.getvalue("name");                      if(!dataitemlist.contains(name))                     {                     dataitemlist.add(name);                     retdata=name;                     }                  }                 if (qname.equalsignorecase("expression")) {                     bexpression = true;                  }              }               public void characters(char ch[], int start, int length) throws saxexception {                  if (bexpression) {                      string exp=new string(ch, start, length);                     if(!expressionlist.contains(exp))                     {                         expressionlist.add(exp);                         retdata+="@@sep@@"+exp;                         retlist.add(retdata);                         retdata="";                     }                     bexpression = false;                 }             }               };              inputsource is=new inputsource(new stringreader(strxml));              saxparser.parse(is, handler); 

but printing text value first expression tag , not printing full text

output coming : expression list = {[gosales_goretailers}

according this question, possible parser call characters() method multiple times.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -