read the third node of xml based on second node in java -


how can read third node of xml product based on second node product group using java. want products based on product code

            <?xml version="1.0" encoding="utf-8"?>             <restaurant>                 <productgroup id="1">                     <label>burgers</label>                     <image_url>burger.png</image_url>                     <product>                         <label>hamburger</label>                         <productcode>aab</productcode>                         <img_url>hamburger.png</img_url>                         <price>129.46</price>                     </product>                     <product>                         <label>cheeseburger</label>                         <productcode>cch</productcode>                         <img_url>cheeseburger.png</img_url>                         <price>129.46</price>                     </product>                       </productgroup>                 <productgroup id="2">                     <label>fries/onion ring</label>                     <image_url>friesonionring.png</image_url>                     <product>                         <label>fries</label>                         <productcode>frs</productcode>                         <img_url>fries.png</img_url>                         <price>50.46</price>                     </product>                     <product>                         <label>onion rings</label>                         <productcode>onr</productcode>                         <img_url>onionring.png</img_url>                         <price>50.46</price>                     </product>                       </productgroup>             </restaurant> 

there's query language designed kind of tasks. it's called xpath. can use select specific nodes xml tree specifying path similar ones know operating system or web addresses. gives ability specify variety of conditions, determine nodes select.

let's take @ couple of examples.

you can use xpath query select product codes group specified value of id attribute:

/restaurant/productgroup[@id='2']/product/productcode  

to product specific id specific group, you'd want add condition. following query requires product element have child element called productcode value of 'aab' :

/restaurant/productgroup[@id='2']/product[productcode='aab'] 

it's possible not specify entire structure of document , require product element somewhere in document , not in productgroup child of restaurant.

//product[productcode='cch'] 

there many tutorials on xpath tha understanding of simple language. it's powerful, easy understand, implemented , not require code verbose.

to use power in java app, check out xpath api: http://ibm.com/developerworks/library/x-javaxpathapi/index.html


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -