title: Print XML tag names and values in Java
body: <p>I have an XML document, and I want to print the tag names and values (of leaf nodes) of all tags in the document.</p>
  
  <p>For example, for the XML:</p>
  
  <pre><code>&lt;library&gt;
    &lt;bookrack&gt;
      &lt;book&gt;
        &lt;name&gt;Book1&lt;/name&gt;
        &lt;price&gt;$10&lt;/price&gt;
      &lt;/book&gt;
      &lt;book&gt;
        &lt;name&gt;Book2&lt;/name&gt;
        &lt;price&gt;$15&lt;/price&gt;
      &lt;/book&gt;
    &lt;/bookrack&gt;
  &lt;/library&gt;
  </code></pre>
  
  <p>The output should be something like:</p>
  
  <pre><code>library=
  bookrack=
  book=
  name=Book1
  price=$10
  book=
  name=Book2
  price=$15
  </code></pre>
  
  <p>Help please!</p>