Java code for dropping elements and attributes

I have a xml document and in that document, I could have a mixture of elements and attributes.

Is there Java code available out there to drop empty elements and attributes? I don’t want to have to go through each elements and/or attributes to find out if their value is null or empty then dropping it.

There must be a better way.

Really roughly, off the top of my head and not compiled (let alone tested):

 
public void dropEmptyValues (IData aDoc)
   {
   IDataCursor vCsr = aDoc.getCursor();
   boolean vOk = vCsr.first();
   while (vOk)
      {
      Object vValue = vCsr.getValue();
      if (vValue == null)
         vOk = vCsr.delete(); // moves to next value if there is one
      else
      if (vValue instanceof String && ((String)vValue).equals(""))
         vOk = vCsr.delete();
      else
         {
         // recurse through child docs
         // beware of reference loops (not checked here)
         if (vValue instanceof IData)
            dropEmptyValues((IData)vValue);
         else
         if (vValue instanceof IData[])
            {
            IData vDocs[] = (IData[])vValue;
            for (int i = 0; i < vDocs.length; i++)
            dropEmptyValues(vDocs[i]);
            }
         vOk = vCsr.next();
         }
      }
   vCsr.destroy();
   }

HTH!