title: How to get elements from XPath in Java
body: <p>I want to get data from an XPath query:</p>
  
  <pre><code>Element location = (Element) doc.query("//location[location_name='"+ locationName +"']/*").get(0).getDocument().getRootElement();
  System.out.println(location.toXML());
  
  Element loc = location.getFirstChildElement("location");
  System.out.println(loc.getFirstChildElement("location_name").getValue());
  </code></pre>
  
  <p>However, no matter what I choose, I always get 1 node (because of <code>.get(0)</code>). I don't know how to select the node which was selected by query.</p>
  
  <p>I found that I should cast the node to Element, (<a href="https://stackoverflow.com/questions/2363599/xom-getting-attribute-from-node">XOM getting attribute from Node?</a>) but the link only shows how to select the first node.</p>