Document doc = getDOMDocument( /* wherever you like */ );
DOMSignContext context
= new DOMSignContext(entry.getPrivateKey(), doc.getDocumentElement());
XMLSignature signature = factory.newXMLSignature(signer, key);
signature.sign(context);
// The Signature element has now been added to the Document.