title: XML parsing using Java with getting element values and attribute values
body: <p>i have an XML file and the elements also have attributes.
  I have a simple java file which is parsing and printing values of elements in a text file but not element attribute values.
  Please can you help in getting the attributes values also to be printed.
  I am pasting the code below:
  --------employees.xml file-----------</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;Personnel&gt;
  
    &lt;Employee type="permanent"&gt;
      &lt;Name&gt;Seagull&lt;/Name&gt;
      &lt;Id&gt;3674&lt;/Id&gt;
      &lt;Age&gt;34&lt;/Age&gt;
     &lt;/Employee&gt;
  
    &lt;Employee type="contract"&gt;
      &lt;Name&gt;Robin&lt;/Name&gt;
      &lt;Id&gt;3675&lt;/Id&gt;
      &lt;Age&gt;25&lt;/Age&gt;
  &lt;/Employee&gt;
  
    &lt;Employee type="permanent"&gt;
      &lt;Name&gt;Crow&lt;/Name&gt;
      &lt;Id&gt;3676&lt;/Id&gt;
      &lt;Age&gt;28&lt;/Age&gt;
    &lt;/Employee&gt;
  
  &lt;/Personnel&gt;
  </code></pre>
  
  <p>----------------------------StoreData.java-----------------------------------------</p>
  
  <pre><code>import java.io.*;
  import org.w3c.dom.*;
  import org.xml.sax.*;
  import javax.xml.parsers.*;
  import javax.xml.transform.*; 
  import javax.xml.transform.dom.DOMSource; 
  import javax.xml.transform.stream.StreamResult;
  
  public class StoreData{
  static public void main(String[] arg) {
      try{
          BufferedReader bf = new BufferedReader(new       InputStreamReader(System.in));
          System.out.print("Enter XML file name: ");
          String xmlFile = bf.readLine();
          File file = new File(xmlFile);
              if (file.exists()){
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document doc = builder.parse(xmlFile);
                          //Create transformer
              Transformer tFormer = TransformerFactory.newInstance().newTransformer();
                       //Output Types (text/xml/html)
              tFormer.setOutputProperty(OutputKeys.METHOD, "text");
  //              Write the document to a file
              Source source = new DOMSource(doc);
  //              Create File  to view your xml data as (vk.txt/vk.doc/vk.xls/vk.shtml/vk.html)
              Result result = new StreamResult(new File("file.txt"));
              tFormer.transform(source, result);
              System.out.println("File creation successfully!");
          }
          else{
              System.out.println("File not found!");
          }
      }
      catch (Exception e){
          System.err.println(e);
          System.exit(0);
      }  
  } }
  </code></pre>
  
  <hr>