• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to parse XML document with JDOM XPath

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello expert,

Could you please help me to parse below xml document using JDom(or any Java API)XPath.

Thanks in Advance.

Dhana
Cisco


This is my xml file:

<key name="bookstore">
<key name="books">
<key name="science">
<key name="0001">
<entry name="auther">auther A</entry>
<entry name="price">$10</entry>
</key>
<key name="0002">
<entry name="auther">auther B</entry>
<entry name="price">$20</entry>
</key>
<key name="0003">
<entry name="auther">auther C</entry>
<entry name="price">$30</entry>
</key>
</key>
<key name="music">
<key name="0001">
<entry name="auther">auther A</entry>
<entry name="price">$10</entry>
</key>
<key name="0002">
<entry name="auther">auther B</entry>
<entry name="price">$20</entry>
</key>
<key name="0003">
<entry name="auther">auther C</entry>
<entry name="price">$30</entry>
</key>
</key>
</key>
</key>


Expected XPath / Result:

#1
bookstore/books/science/0001

result:

<key name="0001">
<entry name="auther">auther A</entry>
<entry name="price">$10</entry>
</key>

#2
bookstore/books/science/0001/auther

result:
auther A

#3
bookstore/books/science

result:
<key name="science">
<key name="0001">
<entry name="auther">auther A</entry>
<entry name="price">$10</entry>
</key>
<key name="0002">
<entry name="auther">auther B</entry>
<entry name="price">$20</entry>
</key>
<key name="0003">
<entry name="auther">auther C</entry>
<entry name="price">$30</entry>
</key>
</key>
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of an XPath expression for your #1:

assuming that your context node is the document element.
 
Dhana Rengasamy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Here's an example of an XPath expression for your #1:

assuming that your context node is the document element.



Thanks a lot Paul...

I got the solution for all my xpath which I mentioned.

The solution:

#1. *[@name='bookstore']/*[@name='books']/*[@name='science']/*[@name='0001']
#2. *[@name='bookstore']/*[@name='books']/*[@name='science']/*[@name='author']
#3. *[@name='bookstore']/*[@name='books']/*[@name='science']
reply
    Bookmark Topic Watch Topic
  • New Topic