java - How I get path in xml -


i have xml structure this.

<?xml version="1.0" encoding="utf-8" ?>  <root>    <child1 name="1"/>    <child2 name="2"/>    <child3 name="3">           <condition>x>=50</condition>           <childofchild3 name="3.1">              <condition>y<40</condition>                 <childofchild3.1 name="3.1.1">                    <condition>a>70</condition>                       <step>                         <a1>                           <aa1> </aa1>                         </a1>                            <b1 />                       </step>                       <c1>                        <a1>                          <aa1> </aa1>                        </a1>                          </c1>              </childofchild3.1>             <c1>               <a1>                 <aa1> </aa1>               </a1>              </c1>           </childofchild3>           <c1>              <a1>                 <aa1> </aa1>              </a1>            </c1>    </child3>    <child4 name="4" />  </root> 

i have print nodes path value using java. here sample of output have get:

child1 -> child2 -> child3 -> childofchild3 -> chileofchild3.1 -> a1 -> b1  -> child4 child1 -> child2 -> child3 -> childofchild3 -> chileofchild3.1 -> c1 -> a1-> child4 child1 -> child2 -> child3 -> childofchild3 -> c1 -> a1-> child4 child1 -> child2 -> child3 -> c1 -> a1-> child4 

this last java code can't work correctly

public class xmlpath{ static list <string []> valuelist = new arraylist<string []>() ; static string rootpath = "";     public static void showpath (){         try{         file xml = new file("test.xml");          string nextnode="";         documentbuilderfactory   dbfactory=documentbuilderfactory.newinstance();         documentbuilder dbuilder = dbfactory.newdocumentbuilder();         document doc = dbuilder.parse(xml);         element root = doc.getdocumentelement();         rootpath += "root";         nodelist sequencelist = doc.getelementsbytagname("root");         element sequencenode = (element) sequencelist.item(0);         nodelist sequencechildlist =  sequencenode.getchildnodes();          for(int i=0;i<sequencechildlist.getlength();i++){               if(sequencechildlist.item(i) instanceof element && sequencechildlist.item(i).getnodetype() == node.element_node){                   nextnode = rootpath+" > "+ sequencechildlist.item(i).getnodename();                       if(sequencechildlist.item(i).haschildnodes()){                         findpath(sequencechildlist.item(i).getchildnodes(), nextnode);                     }                     else{                       if(sequencechildlist.item(i).hasattributes()){                                      namednodemap nameattr = sequencechildlist.item(i).getattributes();                               for(int j=0;j<nameattr.getlength();j++){                                   node node = nameattr.item(j);                                  if ("name".equals(node.getnodename())) {                                     valuelist.add(new string []{nextnode,node.getnodevalue()});                                       }                               }                          }                        }              }         }         iterator <string []> = valuelist.iterator();         for(;it.hasnext();){             string [] val = it.next();             if(!val[0].contains("condition")){             system.out.println(val[0]+" = "+val[1]);           }         }         } catch(exception e ){             e.printstacktrace();         }     }     public static void findpath(nodelist sequencelist,string listpath){         string path, value,count,name=null;         node nextnode;         for(int i=0;i<sequencelist.getlength();i++){               if(sequencelist.item(i) instanceof element){                  path = listpath+" > "+sequencelist.item(i).getnodename();                       if(sequencelist.item(i).getchildnodes().getlength() >=1){                          findpath(sequencelist.item(i).getchildnodes(), path);                     }                      else if(!sequencelist.item(i).haschildnodes()){                         nextnode= nextnode(sequencelist.item(i));                             if(nextnode !=null){                                 findpath(nextnode.getchildnodes(), listpath);                             }                             if(nextnode ==null){                                  system.out.println("not chilenode");                                 valuelist.add(new string []{listpath,name});                             }                        }                                            }         }      }     public static node nextnode(node currentnode){             node nextnode = currentnode.getnextsibling();             if(currentnode.getnodetype() ==node.element_node){                 while(!(nextnode instanceof element) && nextnode != null){                     nextnode = nextnode.getnextsibling();                 }             }             return nextnode;     } 

this actual output.

child1 child2 child3 -> childofchild3 -> childofchild3.1 -> a1 -> aa1 chile3 -> childofchild3 -> childofchild3.1 -> c1 -> a1 -> aa1 child3 -> childofchild3 -> c1 -> a1 -> aa1 child3 -> c1 -> a1 -> aa1 child4 

plese me make correctly. not need use recursion solve problem. thanks.

public static void main(string[] args) throws ioexception, saxexception {         documentbuilder builder = joox.builder();         document xmlexampledocument = builder.parse(xmltest.class.getresourceasstream("/sample.xml"));         list<string> donelist = $(xmlexampledocument).xpath("//*[not(*)]").map(context -> $(context).xpath() + "='" + $(context).text() + "'");         (string x : donelist) {             system.out.println(x);         }     } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

http - Safari render HTML as received -