title: Getting attribute value from node using dom4j
body: <p>My XML is structured like the example below. I'm trying to get the attribute values out of XML using dom4j. </p>
  
  <pre><code>&lt;baz&gt;
    &lt;foo&gt;
      &lt;bar a="1" b="2" c="3" /&gt;
      &lt;bar a="4" b="5" c="6" /&gt;
    &lt;/foo&gt;
  &lt;/baz&gt;
  </code></pre>
  
  <p>Currently the nodes are stored into a List with the following code:</p>
  
  <pre><code>public List&lt;Foo&gt; getFoo() {
    String FOO_XPATH = "//baz/foo/*";
    List&lt;Foo&gt; fooList = new ArrayList&lt;Foo&gt;();
    List&lt;Node&gt; fooNodes = _bazFile.selectNodes(FOO_XPATH);
  
    for (Node n : fooNodes) {
      String a = /* get attribute a */
      String b = /* get attribute b */
      String c = /* get attribute c */
      fooNodes.add(new Foo(a, b, c));
    }
  
    return fooNodes;
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/a/5391828/823435">There is a similar but different question</a> here on SO but that is returning a node's value for a known attribute key/value pair using the following code:</p>
  
  <pre><code>Node value = elem.selectSingleNode("val[@a='1']/text()");
  </code></pre>
  
  <p>In my case, the code knows the keys but doesn't know the values - that's what I need to store. (The above snippet from the similar question/answer also returns a node's text value when I need the attribute value.)</p>