title: Creating a XML parser for Java
body: <p>I'm making a Java application that displays X3D models and I'm creating an XML parser for it. The Java code that I have for it is</p>
  
  <pre><code>package domparserexample.java;
  
  import java.io.IOException;
  import java.util.Iterator;
  import javax.lang.model.element.Element;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import org.w3c.dom.Document;
  import org.w3c.dom.NodeList;
  import org.xml.sax.SAXException;
  
  public class DomParserExampleJava {
  
      private void parseXmlFile() throws IOException,           ParserConfigurationException{
          //get the factory
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  
          try {
  
              //Using factory get an instance of document builder
              DocumentBuilder db = dbf.newDocumentBuilder();
  
                  //parse using builder to get DOM representation of the XML file
                  Document dom = db.parse("employees.xml");
  
  
          }catch(SAXException se) {
              se.printStackTrace();
          }catch(IOException ioe) {
              ioe.printStackTrace();
          }
      }
  
      private void parseDocument(){
          //get the root element
          Element docEle = dom.getDocumentElement();
  
          //get a nodelist of elements
          NodeList nl = docEle.getElementsByTagName("Employee");
          if(nl != null &amp;&amp; nl.getLength() &gt; 0) {
              for(int i = 0 ; i &lt; nl.getLength();i++) {
  
                  //get the employee element
                  Element el = (Element)nl.item(i);
  
                  //get the Employee object
                  Employee e = getEmployee(el);
  
                  //add it to list
                  myEmpls.add(e);
              }
          }
      }
      /**
      * I take an employee element and read the values in, create
      * an Employee object and return it
      */
      private Employee getEmployee(Element empEl) {
  
          //for each &lt;employee&gt; element get text or int values of
          //name ,id, age and name
          String name = getTextValue(empEl,"Name");
          int id = getIntValue(empEl,"Id");
          int age = getIntValue(empEl,"Age");
  
          String type = empEl.getAttribute("type");
  
          //Create a new Employee with the value read from the xml nodes
          Employee e = new Employee(name,id,age,type);
  
          return e;
      }
  
  
      private String getTextValue(Element ele, String tagName) {
          String textVal = null;
          NodeList nl = ele.getElementsByTagName(tagName);
          if(nl != null &amp;&amp; nl.getLength() &gt; 0) {
              Element el = (Element)nl.item(0);
              textVal = el.getFirstChild().getNodeValue();
          }
  
          return textVal;
      }
      private int getIntValue(Element ele, String tagName) {
          //in production application you would catch the exception
          return Integer.parseInt(getTextValue(ele,tagName));
      }
      private void printData(){
  
          System.out.println("No of Employees '" + myEmpls.size() + "'.");
  
          Iterator it = myEmpls.iterator();
          while(it.hasNext()) {
              System.out.println(it.next().toString());
          }
      }
  }
  </code></pre>
  
  <p>And the XML code is: </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>However, I'm getting some errors on lines 49, 76, 93, and 96 in the DomParserExample.java file and I don't know why it's happening. All the errors say that they <code>cannot find symbol</code> and <code>symbol: method getElemantsByTagName(String)</code>, <code>symbol: method getAttribute(String)</code>, <code>symbol: method getElementByTagName(String)</code>, and <code>symbol: method getFirstChild()</code>.</p>