title: How to extract attribute values with XPath
body: <p>I have the following xml document and now I want to transform the station element with all attribute values in java object:</p>
  
  <pre><code>&lt;LocValRes flag="FINAL" id="dummy"&gt;
      &lt;Station name="Sampieri" distance="2901857" duration="3482348" externalId="008302686#80" externalStationNr="008302686" type="WGS84" x="14744601" y="36731901"/&gt;
      &lt;Station name="Pozzallo" distance="2903750" duration="3484620" externalId="008302642#80" externalStationNr="008302642" type="WGS84" x="14847168" y="36733609"/&gt;
      &lt;Station name="Scicli" distance="2907477" duration="3489092" externalId="008302695#80" externalStationNr="008302695" type="WGS84" x="14699187" y="36790088"/&gt;
      &lt;Station name="Ispica" distance="2909739" duration="3491806" externalId="008302562#80" externalStationNr="008302562" type="WGS84" x="14914362" y="36778043"/&gt;
      &lt;Station name="Rosolini" distance="2914534" duration="3497560" externalId="008302660#80" externalStationNr="008302660" type="WGS84" x="14952836" y="36815950"/&gt;
      &lt;Station name="Modica" distance="2915249" duration="3498418" externalId="008300462#80" externalStationNr="008300462" type="WGS84" x="14754166" y="36852950"/&gt;
      &lt;Station name="Donnafugata" distance="2915468" duration="3498681" externalId="008302530#80" externalStationNr="008302530" type="WGS84" x="14568817" y="36882183"/&gt;
      &lt;Station name="Ragusa" distance="2922026" duration="3506551" externalId="008300336#80" externalStationNr="008300336" type="WGS84" x="14725427" y="36919209"/&gt;
      &lt;Station name="Comiso" distance="2923096" duration="3507835" externalId="008302524#80" externalStationNr="008302524" type="WGS84" x="14600540" y="36947283"/&gt;
      &lt;Station name="Vittoria" distance="2923168" duration="3507921" externalId="008300491#80" externalStationNr="008300491" type="WGS84" x="14524959" y="36958762"/&gt;
      &lt;Station name="Noto" distance="2923895" duration="3508794" externalId="008302609#80" externalStationNr="008302609" type="WGS84" x="15074307" y="36882498"/&gt;
      &lt;Station name="Acate" distance="2925789" duration="3511066" externalId="008302445#80" externalStationNr="008302445" type="WGS84" x="14425430" y="36996652"/&gt;
      &lt;Station name="Avola" distance="2927532" duration="3513158" externalId="008302461#80" externalStationNr="008302461" type="WGS84" x="15125762" y="36907586"/&gt;
      &lt;Station name="Gela-Anic" distance="2929204" duration="3515164" externalId="008302743#80" externalStationNr="008302743" type="WGS84" x="14299176" y="37045013"/&gt;
      &lt;Station name="Licata" distance="2930810" duration="3517092" externalId="008300380#80" externalStationNr="008300380" type="WGS84" x="13938278" y="37105987"/&gt;
      &lt;Station name="Gela" distance="2931688" duration="3518145" externalId="008300310#80" externalStationNr="008300310" type="WGS84" x="14259255" y="37072988"/&gt;
      &lt;Station name="Falconara" distance="2933198" duration="3519957" externalId="008302532#80" externalStationNr="008302532" type="WGS84" x="14052468" y="37113511"/&gt;
      &lt;Station name="Niscemi" distance="2941720" duration="3530184" externalId="008300423#80" externalStationNr="008300423" type="WGS84" x="14403505" y="37144992"/&gt;
      &lt;Station name="Siracusa" distance="2947889" duration="3537586" externalId="008300338#80" externalStationNr="008300338" type="WGS84" x="15281733" y="37068907"/&gt;
      &lt;Station name="Campobello Ravanusa" distance="2948468" duration="3538281" externalId="008302485#80" externalStationNr="008302485" type="WGS84" x="13946350" y="37265582"/&gt;
  &lt;/LocValRes&gt;
  </code></pre>
  
  <p>I mean that I need for each station xml element a own java object. That is no problem, I have a solution for this task. Now I want to extract each attribute value with XPath. I have written the following java code:</p>
  
  <pre><code> private static String getAttributValue(String attribute, String xmlResponseBody) {
  
      String searchAttributeValue = "";
      try {
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          Document doc = docBuilder.parse(new InputSource(new StringReader(xmlResponseBody)));
          XPathFactory xPathFactory = XPathFactory.newInstance();
          XPath xpath = xPathFactory.newXPath();
          try {
              XPathExpression expr = xpath.compile("//@" + attribute);
              Object result = expr.evaluate(doc, XPathConstants.NODESET);
              NodeList nodeList = (NodeList) result;
              for (int i = 0; i &lt; nodeList.getLength(); i++) {
                  searchAttributeValue = nodeList.item(i).getTextContent();
              }
          } catch (XPathExpressionException e) {
              e.printStackTrace();
          }
      } catch (ParserConfigurationException pce) {
          pce.printStackTrace();
      } catch (IOException ioe) {
          ioe.printStackTrace();
      } catch (SAXException sae) {
          sae.printStackTrace();
      }
      return searchAttributeValue;
  }
  </code></pre>
  
  <p>But the Loop ...</p>
  
  <pre><code>for (int i = 0; i &lt; nodeList.getLength(); i++) {
              searchAttributeValue = nodeList.item(i).getTextContent();
          }
  </code></pre>
  
  <p>... extract only the value from the last station element. I think i need a position prädicate in my XPath expression, maybe quite similar then this:</p>
  
  <pre><code>//@" + attribute + [position() + i +]
  </code></pre>
  
  <p>How can I extract each attribute value of each available station element in this Java For Loop code?</p>