java - Issue in XML parsing using SAX -


i have xml trying parse.

<tests>    <test>      <blocks>          <block>             <blockid>2</blockid>             <name>cccc</name>             <type>action</type>             <taskid>2</taskid>             <send>                <wid>284</wid>                <blockid>14</blockid>             </send>          </block>          <block>             <blockid>10</blockid>             <name>start vm4</name>             <type>action</type>             <taskid>10</taskid>             <send />          </block>          <block>             <blockid>12</blockid>             <name>shut</name>             <type>action</type>             <taskid>12</taskid>             <send />          </block>      </blocks>  </tests> </test> 

i using sax parse this. works fine, every time loop through, should block id 2 , block blockid 10 , 12. , adding these blocks test.

portion of code is:

public void startelement(string uri, string localname, string qname,         attributes attributes) throws saxexception {     nqname = qname;     tag_name_list.setelementat(nqname, level);     level = level + 1;  }  public void endelement(string uri, string localname,         string qname) throws saxexception {     level = level - 1;     tag_name_list.removeelementat(level); }  public void characters(char ch[], int start, int length) throws saxexception {      if (level != 0) {         ////////////////some code     } else if (level == 5             && tag_name_list.elementat(1).equals("test")              && tag_name_list.elementat(2).equals("blocks")              && tag_name_list.elementat(3).equals("block")              && (nqname.equalsignorecase("blockid"))) {         block = new block();         test.addblock(block);         block.setid(new string(ch, start, length));         block.setworkflowid(workflow.getid());      } else if (level == 5 && ...) {           ////// code continues 

nb huge xml , huge code, sharing partly...

but issue here is:

  • the first time id 2,
  • then "\n "
  • then again id 10
  • and "\n "
  • then id 12
  • and "\n ".

i not sure why getting these "\n ".

i can put if condition avoid entity, if lose information attached id, later gets associated "\n " id.

has faced , can give pointer.

let me know if more information needed.

after debugging code found that, taking "\n " end of

<blockid>14</blockid> 

since there \r , "\n " next line here.

how can avoid this?

you assign nqname = qname. ever change value until next iteration?

if don't change value when leave context of blockid element, still equal to blockid when outside element not yet inside name, example. , characters() method read all whitespace finds there.

there should reset nqname in endelement(). try adding

nqname = null; 

to endelement() method.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -