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.
Here's a start, using Guava MultiMap:
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?