title: DOM - Missing Element in XML Document
body: <p>The XML file contains Employee (with empID, empName, empCode). In some situation empCode is missing.        </p>
  
  <pre><code>&lt;Employee&gt;&lt;Detail&gt;&lt;empID&gt;1&lt;/empID&gt;&lt;empName&gt;Abhi&lt;/empName&gt;&lt;empCode&gt;One&lt;/empCode&gt;
  &lt;/Detail&gt;
  
  &lt;Detail&gt;&lt;empID&gt;2&lt;/empID&gt;&lt;empName&gt;Amit&lt;/empName&gt;
  &lt;/Detail&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>I am getting the Null pointer Exception while calling the getTagValue() method for "empCode" as there is no tag available with the name in XML.</p>
  
  <p>Java Code : </p>
  
  <p>try
           {
               File xmlFile = new File("New.xml");</p>
  
  <pre><code>         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(xmlFile);
              doc.getDocumentElement().normalize();
  
              NodeList nList = doc.getElementsByTagName("Detail");
              for (int temp = 0; temp &lt; nList.getLength(); temp++) 
              {
                  Node nNode = nList.item(temp);
                  if (nNode.getNodeType() == Node.ELEMENT_NODE) 
                  {
                      Element eElement = (Element) nNode;
                      EmpDetail empInfo = new EmpDetail();
  
                      empInfo.SetEmpID(getTagValue("empID", eElement));
                      empInfo.SetEmpName(getTagValue("empName",eElement));
                      empInfo.SetEmpCode(getTagValue("empCode",eElement));
  
                      DBConnector.SaveinDB(empInfo);
                  }
  
              }
       }
       catch (Exception e) 
       {
           System.out.println("Error: ");
           e.printStackTrace();
       }
   }
  
   private static String getTagValue(String sTag, Element eElement) 
   {
          NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
          Node nValue = (Node) nlList.item(0);
          if(nValue == null) 
              return null;
          return insertEscapeSequance(nValue.getNodeValue());
        }
  
   private static String insertEscapeSequance(String str)
   {
       String returnstr = "";
       String[] strarr = str.split("'");
       returnstr = strarr[0];
       for(int i=1;i&lt;strarr.length;i++)
       {
           returnstr = returnstr + "\\'" + strarr[i];
       }
       return returnstr;
  }
  </code></pre>
  
  <p>Now I want to save the XML data into sql like this :</p>
  
  <p>1   Abhi    One
  2   Amit    null</p>
  
  <p>I tried so many links but not success. Can someone please help me</p>