To parse XML using XPATH -


i have below xml. want retrieve item id , batters when topping id = '5002'. kindly help.

    <?xml version="1.0" encoding="utf-8"?>     <items>         <item id="0001" type="donut">             <name>cake</name>             <ppu>0.55</ppu>             <batters>                 <batter id="1001">regular</batter>                 <batter id="1002">chocolate</batter>                 <batter id="1003">blueberry</batter>                 <batter id="1003">devil's food</batter>             </batters>             <topping id="5001">none</topping>             <topping id="5002">glazed</topping>             <topping id="5005">sugar</topping>             <topping id="5007">powdered sugar</topping>             <topping id="5006">chocolate sprinkles</topping>             <topping id="5003">chocolate</topping>             <topping id="5004">maple</topping>         </item>     </items> 

here piece of code i'm not able retrieve item id , batters.

string expression1 = "/items/item/topping[@id='5002']"; nodelist nodelist1 = (nodelist) xpath.compile(expression1).evaluate(doc, xpathconstants.nodeset);  (int = 0; < nodelist1.getlength(); i++) {     node nnode = nodelist1.item(i);      system.out.println("\ncount :"+count);      if (nnode.getnodetype() == node.element_node)      {         element eelement = (element) nnode;            system.out.println("item id : "+ eelement.getattribute("id"));         //system.out.println("item name : "+ eelement.getelementsbytagname("name").item(0).gettextcontent());                 //system.out.println("item ppu : "+ eelement.getelementsbytagname("ppu").item(0).gettextcontent());                 //system.out.println("item batters : "+ eelement.getelementsbytagname("batters").item(0).gettextcontent());                 system.out.println("item topping : "+ eelement.gettextcontent());                 count++;     } } 

i assume want item id , batters can use following xpath :

//topping[contains(@id,'5002')]/parent::* | //topping[contains(@id,'5002')]/preceding-sibling::*/batter

java code :

string expression1 = "//topping[contains(@id,'5002')]/parent::* | //topping[contains(@id,'5002')]/preceding-sibling::*/batter";  nodelist nodelist1 = (nodelist) xpath.compile(expression1).evaluate(doc, xpathconstants.nodeset);  (int = 0; < nodelist1.getlength(); i++)  {         node nnode = (node) nodelist1.item(i);          if (nnode.getnodetype() == node.element_node)          {             element eelement = (element) nnode;                if ( eelement.getnodename().equals("item"))             {                 system.out.println("item id : "+ eelement.getattribute("id"));             }             else if ( eelement.getnodename().equals("batter"))             {                 system.out.println("batter : " + eelement.gettextcontent() );             }          } } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -