get prefix / namespace bindings used in an XML document preferably using org.w3c.dom and javax.xml APIs

4.1k views Asked by At

Is there a way to get the prefix / namespace bindings used in a XML document preferably using standard org.w3c.org and javax.xml APIs? I understand that prefixes may by re-defined in contained elements so the API should be able to account for that as well.

I am looking for a code snipped that would take an XML document in some serialized form and return a Map<String, String> of prefix to URI name bindings (plus some extra cleverness in the API to account for redefinitions in enclosed elements). Ideally a library might be able to do fancier stuff such as identify / purge unused bindings, move duplicate bindings to their nearest common ancestor (instead of having them replicated all over the place) and so on.

1

There are 1 answers

4
forty-two On BEST ANSWER

Here's a start, using Guava MultiMap:

Document doc = ...;

Multimap<String, String> bindings = LinkedHashMultimap.create();
DocumentTraversal dt = (DocumentTraversal) doc;
NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT,
        null, false);
Element element = (Element) i.nextNode();
while (element != null) {
    String prefix = element.getPrefix();
    if (prefix != null) {
        String uri = element.getNamespaceURI();
        bindings.put(prefix, uri);
    }
    element = (Element) i.nextNode();
}

This will only pick up the bindings that are in effect, and it will not bind any default namespace (that can be fixed of course). Re-definitions of bindings will be represented in document order, but not the depth where they occurred.

I guess any further refinement of this method depends on your use case, e.g. what more information do you nedd to make something sensible of a re-defined binding?