title: Parsing an XML file in Java to get a list of names
body: <p>I am currently working on an Android app and that means learning Java. I've toyed around with Python for a few years, but decided it was time to step up now that I have an android phone. The app basically displays a list of video games stored in an XML file locally. Right now, the structure of the XML file is basically games>game (Multiple)>name (Plus other things not important now). I am currently trying to get a list of the names of the games. I've looked up tutorials and info but none of it seems to be quite what I need. I want to actually understand how it works, not just have a working piece of code I can copy/paste. Also, keep in mind that the list of names has to end up as an array of strings for Android to use it. Here's the function I have right now (Copy/pasted from a tutorial and heavily edited, so it's not readable. I'll fix that once it's actually working.) Right now the listview shows up as empty. At least it's better than before and it doesn't crash anymore though...</p>
  
  <pre><code>public static String[] parse(String filename) {
        ArrayList&lt;String&gt; gamesList = new ArrayList&lt;String&gt;();
  
        try {
        File file = new File(filename);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("game");
  
        for (int s = 0; s &lt; nodeList.getLength(); s++) {
  
          Node fstNode = nodeList.item(s);
  
          //if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
            Element name = (Element) fstNode;
                 Element fstElmnt = (Element) fstNode;
            NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("name");
            Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
            NodeList fstNm = fstNmElmnt.getChildNodes();
  
            gamesList.add(fstNmElmnt.toString());
          //}
  
        }
        } catch (Exception e) {
          e.printStackTrace();
        }
      String[] gamesArray;
      gamesArray = (String[]) gamesList.toArray(new String[0]);
      return gamesArray;
       }
  </code></pre>