Issue with upadating xml file using java? -


//**am getting null @ nodelist nlist = doc.getelementsbytagname(e); below xml content trying delete similar tags present in xml file. nlist showing null. how approach problem?

**

    public class delelement { public static string parent; public static void main(string[] args) {      try {          file file = new file("d:/vodafone/parse.xml");          documentbuilder dbuilder = documentbuilderfactory.newinstance()         .newdocumentbuilder();          document doc = dbuilder.parse(file);          system.out.println("root element :" + doc.getdocumentelement().getnodename());         parent=doc.getdocumentelement().getnodename();         if (doc.haschildnodes()) {              printnote(doc,doc.getchildnodes());          }      } catch (exception e) {         system.out.println(e.getmessage());     }  }  private static void printnote(document doc, nodelist nodelist) throws transformerexception {        string file = "d:/vodafone/parse.xml";      (int count = 0; count < nodelist.getlength(); count++) {          node tempnode = nodelist.item(count);          // make sure it's element node.         if (tempnode.getnodetype() == node.element_node) {              // node name , value             system.out.println("\nnode name =" + tempnode.getnodename() + " [open]");             system.out.println("node value =" + tempnode.gettextcontent());              if(tempnode.getnodename()!=parent){                      system.out.println("----------------------------");                    if (tempnode.getchildnodes().getlength()>1){                     nodelist nodemap=tempnode.getchildnodes();                     system.out.println("inside del");                      (int i=0;i<nodemap.getlength();i++){                            string e = "'"+(string)nodemap.item(i).getnodename()+"'";                          system.out.println("inside del node:"+e);                         nodelist nlist = doc.getelementsbytagname(e);                          if (nlist.getlength()>1){                              (int con=1;i==nlist.getlength()-1;con++){                                  element el = (element)nlist.item(con);                                 system.out.println("element"+el);                                 system.out.println("parent"+el.getparentnode());                                 el.getparentnode().removechild(el);                             }                             transformerfactory transformerfactory = transformerfactory.newinstance();                              transformer transformer = transformerfactory.newtransformer();                             transformer.setoutputproperty(outputkeys.indent, "yes");                             domsource source = new domsource(doc);                             streamresult result = new streamresult(new file(file));                             transformer.transform(source, result);                         }                     }}}              if (tempnode.haschildnodes()) {                  // loop again if has child nodes                 printnote(doc, tempnode.getchildnodes());              }              system.out.println("node name =" + tempnode.getnodename() + " [close]");          }      }  }} 

the problem seems in "for loop" trying delete nodes. loop says:: "for (int con=1;i==nlist.getlength()-1;con++){" ... based on code, condition break loop "i==nlist.getlength()-1" true never ends going inside block...

i believe, should doing (still feels little convoluted way small job; i'm guessing sharing part of puzzle... see below; delete nodes...

                        (int con=nlist.getlength()-1; con>=0; con--) {                             element el = (element)nlist.item(con);                             system.out.println("element"+el);                             system.out.println("parent"+el.getparentnode());                             el.getparentnode().removechild(el);                         }                         /*                         (int con=1;i==nlist.getlength()-1;con++){                              element el = (element)nlist.item(con);                             system.out.println("element"+el);                             system.out.println("parent"+el.getparentnode());                             el.getparentnode().removechild(el);                         }                         */ 

hope helps.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -