code: public static NamespaceContext
          toNamespaceContext(Map<String, String> prefix2URI) {
          final Map<String, String> copy =
              new LinkedHashMap<String, String>(prefix2URI);
          return new NamespaceContext() {
              public String getNamespaceURI(String prefix) {
                  if (prefix == null) {
                      throw new IllegalArgumentException("prefix must not be null");
                  }
                  if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
                      return XMLConstants.XML_NS_URI;
                  }
                  if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
                      return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
                  }
                  String uri = copy.get(prefix);
                  return uri != null ? uri : XMLConstants.NULL_NS_URI;
              }
  
              public String getPrefix(String uri) {
                  Iterator i = getPrefixes(uri);
                  return i.hasNext() ? (String) i.next() : null;
              }
  
              public Iterator getPrefixes(String uri) {
                  if (uri == null) {
                      throw new IllegalArgumentException("uri must not be null");
                  }
                  Collection<String> c = new LinkedHashSet<String>();
                  boolean done = false;
                  if (XMLConstants.XML_NS_URI.equals(uri)) {
                      c.add(XMLConstants.XML_NS_PREFIX);
                      done = true;
                  }
                  if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) {
                      c.add(XMLConstants.XMLNS_ATTRIBUTE);
                      done = true;
                  }
                  if (!done) {
                      for (Map.Entry<String, String> entry : copy.entrySet()) {
                          if (uri.equals(entry.getValue())) {
                              c.add(entry.getKey());
                          }
                      }
                  }
                  return c.iterator();
              }
          };
      }
docs: Creates a JAXP NamespaceContext from a Map prefix =&gt; Namespace URI.