title: Unable to do the add, delete nodes in DOM XML document
body: <p>Actually i am working on the XML DOM parser for the bellow xml string</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;schema version="1.0"&gt;
    &lt;itemGroups&gt;
      &lt;itemGroup id="itemGroup1"&gt;
        &lt;items&gt;
          &lt;item id="One" /&gt;
          &lt;item id="Two" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
        &lt;/items&gt;
      &lt;/itemGroup&gt;
      &lt;itemGroup id="itemGroup2"&gt;
        &lt;items&gt;
          &lt;item id="One" /&gt;
          &lt;item id="Two" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
          &lt;item id="item1" /&gt;
        &lt;/items&gt;
      &lt;/itemGroup&gt;
    &lt;/itemGroups&gt; 
  &lt;/schema&gt;
  </code></pre>
  
  <p>Here from each itemGroup i need to remove the items which has the id like 'item'. Please help me to do this thing by using Document object only.</p>
  
  <p>This is the Code we are using to remove the element but getting null pointer exception</p>
  
  <pre><code>public static Document removeElement(Document doc, String idValue,
              String groupNode, String itemNode) {
          NodeList itemGroupList = doc.getElementsByTagName(groupNode);
          for (int i = 0; i &lt; itemGroupList.getLength(); i++) {
              Element node = (Element) itemGroupList.item(i);
              if (node.getAttribute("id").equals(idValue)) {
                  NodeList itemlist = node.getElementsByTagName(itemNode);
                  for (int j = 0; j &lt; itemlist.getLength(); j++) {
                      Element nodenewitm = (Element) itemlist.item(j);
                      NodeList nodeListRemove = nodenewitm.getElementsByTagName("item");
                      if(nodenewitm != null) {
                          NodeList itemsList = nodenewitm.getChildNodes();
                          if(itemsList != null) {
                              for (int counter = 0; counter &lt; itemsList.getLength(); counter++) {
                                  Element item = (Element) itemlist.item(counter);
                                  if(item.getAttribute("id").indexOf("ATTR_") &gt; 0) {
                                      nodenewitm.removeChild(item);
                                  }
                              }
                          }
                      }
                  }
              }
          }
          return doc;
      }
  </code></pre>
  
  <p>We are just calling removeElement(document, "itemGroup1", "itemGroup", "items");</p>
  
  <p>I appreciate if i get help on this soon.</p>
  
  <p>Thanks
  Ramesh Reddy</p>