title: Operations on Nodes
body: <p>I am learning the Java XML API. I am using DOM. </p>
  
  <p>I have a problem with even basic navigation inside the document. Here is the XML files I am working with:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;company&gt;
      &lt;staff&gt;
          &lt;firstname&gt;test&lt;/firstname&gt;
          &lt;lastname&gt;test2&lt;/lastname&gt;
          &lt;nickname&gt;test3&lt;/nickname&gt;
          &lt;salary&gt;test4&lt;/salary&gt;
      &lt;/staff&gt;
      &lt;staff&gt;
          &lt;firstname&gt;test5&lt;/firstname&gt;
          &lt;lastname&gt;test6&lt;/lastname&gt;
          &lt;nickname&gt;test7&lt;/nickname&gt;
          &lt;salary&gt;test8&lt;/salary&gt;
      &lt;/staff&gt;
  &lt;/company&gt;
  </code></pre>
  
  <p>And here is the code that I have so far and supposed to get the name of the parent node and it's child nodes:</p>
  
  <pre><code>DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder  = factory.newDocumentBuilder();
  Document document = builder.parse(new File(pathtothefile));
  
  Element topLevelElement = document.getDocumentElement();
  NodeList secondLevelElements = topLevelElement.getChildNodes();
  
  System.out.println("Top level element: " + topLevelElement.getNodeName());
  System.out.println("Number of second level nodes: " + secondLevelElements.getLength());
  System.out.println("Node at index 0: " + secondLevelElements.item(0).getNodeValue());
  </code></pre>
  
  <p>I get number of second level nodes (it is for some reason 5, not 2) but when I try to get the name of node at index 0 I get "#text" or if I try to get the value: nothing displays.</p>
  
  <p>I would appreciate any help as I am a total beginner to all of these and fell kind of lost :)</p>
  
  <p>UPDATE 1:</p>
  
  <p>Here is the new code:</p>
  
  <pre><code>Element companyElement = document.getDocumentElement();
  NodeList staffElements = companyElement.getElementsByTagName("staff");
  NodeList firstNameElements = companyElement.getElementsByTagName("firstname");
  NodeList lastNameElements = companyElement.getElementsByTagName("lastname");
  NodeList nicknameElements = companyElement.getElementsByTagName("nickname");
  NodeList salaryElements = companyElement.getElementsByTagName("salary");
  
  System.out.println("Top level element: " + companyElement.getNodeName());
  System.out.println("----");
  System.out.println("Next nodes' level name: " + staffElements.item(0).getNodeName());
  System.out.println("Next nodes' level number: " + staffElements.getLength());
  System.out.println("----");
  System.out.println("Person No. 1");
  System.out.println("First name: " + firstNameElements.item(0).getNodeValue());
  System.out.println("Last name: " + lastNameElements.item(0).getNodeValue());
  System.out.println("Nickname: " + nicknameElements.item(0).getNodeValue());
  System.out.println("Salary: " + salaryElements.item(0).getNodeValue());
  System.out.println("----");
  System.out.println("Person No. 2");
  System.out.println("First name: " + firstNameElements.item(1).getNodeValue());
  System.out.println("Last name: " + lastNameElements.item(1).getNodeValue());
  System.out.println("Nickname: " + nicknameElements.item(1).getNodeValue());
  System.out.println("Salary: " + salaryElements.item(1).getNodeValue());
  </code></pre>