Choosing Java xpath

I'm having trouble getting values ​​from an XML document. The document is as follows:

<marketstat>  

<type id="35"> 
  <sell> 
    <median>6.00</median>  
  </sell> 
</type>  

<type id="34">    
  <sell> 
    <median>2.77</median> 
  </sell> 
</type>      

</marketstat> 

I need to get the median, where type = x.

I always had to deal with xpath with Java, and I could never find good tutorials or links for this. If anyone could help me figure this out, that would be great.

+3
source share
4 answers

For the XPATH side of the problem: this should do the trick

//*/type[@id='34']/*/median

When I deal with XPATH, I usually use W3Schools as a reference. IBM DeveloperWorks has a tutorial for the Java XPath API

+2
source

XQuery. x, :

for $var in doc('this_file.xml')//type
where $var/@id='x'
return data($var/sell/median)

XQuery:

0

If you are new to Xpath, you can install the FireXpath plugin in Firefox. This is a great way to test your xpath expressions - and learn xPath.

0
source

Another way to select the median node can be:

//type[@id='34']//median

You can find many examples of xpath requests at http://www.zvon.org/xxl/XPathTutorial/General/examples.html

0
source

Source: https://habr.com/ru/post/1750228/


All Articles