public String getNamespacePrefix(String uri) { NamespaceContextIterator eachNamespace = getNamespaceContextNodes(); while (eachNamespace.hasNext()) { org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr(); if (namespaceDecl.getNodeValue().equals(uri)) { String candidatePrefix = namespaceDecl.getLocalName(); if ("xmlns".equals(candidatePrefix)) return ""; else return candidatePrefix; } } // Find if any of the ancestors' name has this uri org.w3c.dom.Node currentAncestor = this; while (currentAncestor != null && !(currentAncestor instanceof Document)) { if (uri.equals(currentAncestor.getNamespaceURI())) return currentAncestor.getPrefix(); currentAncestor = currentAncestor.getParentNode(); } return null; }