dom - Conditional Java Parsing - Getting Child Node Contents -
i having problems should simple dom parsing. have checked on numerous questions , far nothing has helped situation. problem have conditional nodes may appear in xml or may not appear. tool have created must save contents of these values arraylists used later. here xml in question:
-<dbtr> -<pstladr> <adrline>111 arlington ave</adrline> <adrline>apartment a</adrline> <adrline>augusta, az 11100</adrline> </pstladr> </dbtr>
specifically, dbtr tag may appear number of times in xml. each dbtr tag there may between 1-4 adrline children. need able save value of each adrline , if there no value save blank "" value array list each.
to wrote following code:
nodelist dbtr = doc.getelementsbytagname("dbtr"); for(int = 0; < dbtr.getlength(); i++){ nodelist dbtrchildren = dbtr.item(i).getchildnodes(); if(dbtrchildren.getlength()==1){ //add first child. }else if(dbtrchildren.getlength()==2){ //add first & second child. }else if(dbtrchildren.getlength()==3){ system.out.println("test flag"); system.out.println(dbtrchildren.item(0).getnodevalue()+"node value"); system.out.println(dbtrchildren.item(0).getattributes()+"text attributes"); system.out.println(dbtrchildren.item(0).gettextcontent()+"text content"); }else if(dbtrchildren.getlength()==4){ //add 4 children. } }
so depending on number of children adrline nodes values either saved array list or else blank value saved.
the problem no matter blank values children. can see during testing dbtr tag in fact have 3 children. can see tried debugging figure out way values. see results below:
test flag node value nulltext attributes text content
so i'm getting large amount of whitespace no value. of course considered perhaps picking "pstladr" why detect 3 child nodes?
any appreciated.
i figured out solution problem.
in above example getting "3" length of dbtrchildren led me believe 3 children elements adrline tags.
in reality 3 children #text, pstladr , #text (the #text apparently represents /n).
so trying values tag without values. once tried
for(int = 0; < dbtr.getlength(); i++){ nodelist dbtr2 = dbtr.item(i).getchildnodes(); node dbtr3 = dbtr2.item(1); system.out.println(dbtr3.getnodename()); }
i got "pstladr" result know need go further "pstladr" fix problem.
Comments
Post a Comment