title: Trying to parse an XML File in Java and store variables in an ArrayList
body: <p>I am trying to parse the following XML file in Java and to store the XTimeStamp variable in an ArrayList and YVoltage in another ArrayList:</p>
  
  <pre><code>&lt;?xml version = "1.0"?&gt;
  &lt;Data&gt;
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:01&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  0.6&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:02&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  0.5&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:025&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  0.5&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:031&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  0.1&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:039&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  -0.1&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:00:050&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  -0.2&lt;/YVoltage&gt;
  &lt;/reading&gt;
  
  
  &lt;reading&gt;
      &lt;XTimeStamp&gt; 12:00:01:01&lt;/XTimeStamp&gt;
      &lt;YVoltage&gt;  0.01&lt;/YVoltage&gt;
  &lt;/reading&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>Here is the Java code I currently have to accomplish the task:</p>
  
  <pre><code>import java.util.*;
  import java.io.File;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class XMLReader {
  
      public static void main(String argv[]) {
          ArrayList XTimeStamp = new ArrayList();
          ArrayList YVoltage = new ArrayList();
  
          try {
              File file = new File("C:\\Users\\user\\My Documents\\MyXMLFile.xml");
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc = db.parse(file);
              doc.getDocumentElement().normalize();
              System.out.println("Root element "
                      + doc.getDocumentElement().getNodeName());
              NodeList nodeLst = doc.getElementsByTagName("reading");
              System.out.println("Data");
  
              for (int s = 0; s &lt; nodeLst.getLength(); s++) {
  
                  Node fstNode = nodeLst.item(s);
  
                  if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
                      Element fstElmnt = (Element) fstNode;
                      NodeList fstNmElmntLst = fstElmnt
                              .getElementsByTagName("XTimeStamp");
                      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                      NodeList fstNm = fstNmElmnt.getChildNodes();
                      Element TimeStamp = (Element) fstNmElmntLst.item(0);
                      XTimeStamp.add(TimeStamp);
                      System.out.println("XTimeStamp : "
                              + ((Node) fstNm.item(0)).getNodeValue());
                      NodeList lstNmElmntLst = fstElmnt
                              .getElementsByTagName("YVoltage");
                      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                      NodeList lstNm = lstNmElmnt.getChildNodes();
                      Element YValue = (Element) lstNm;
                      YVoltage.add(YValue);
                      System.out.println("YVoltage : "
                              + ((Node) lstNm.item(0)).getNodeValue());
                  }
  
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          System.out.println(XTimeStamp);
          System.out.println(YVoltage);
      }
  }
  </code></pre>
  
  <p>When I run the code I am getting the following output, as can be seen from the listings of the ArrayLists at the bottom there is a problem:</p>
  
  <pre><code>Root element Data
  Data
  XTimeStamp :  12:00:00:01
  YVoltage :   0.6
  XTimeStamp :  12:00:00:02
  YVoltage :   0.5
  XTimeStamp :  12:00:00:025
  YVoltage :   0.5
  XTimeStamp :  12:00:00:031
  YVoltage :   0.1
  XTimeStamp :  12:00:00:039
  YVoltage :   -0.1
  XTimeStamp :  12:00:00:050
  YVoltage :   -0.2
  XTimeStamp :  12:00:01:01
  YVoltage :   0.01
  [[XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null],  [XTimeStamp: null], [XTimeStamp: null]]
  [[YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null]]
  </code></pre>
  
  <p>Any help with this would be much appreciated.</p>
  
  <p>Thanks.</p>