private void addNamespaces(Namespaces rv, Element element) { if (element == null) throw new RuntimeException("element must not be null"); String myDefaultNamespace = toUri(element.lookupNamespaceURI(null)); String parentDefaultNamespace = ""; if (element.getParentNode() != null) { parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null)); } if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element)) { rv.declare(Namespace.create("", myDefaultNamespace)); } NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) { rv.declare(Namespace.create(attr.getLocalName(), attr.getValue())); } } }
private Namespaces getAllNamespaces() { Namespaces rv = new Namespaces(); Node target = this.dom; if (target instanceof Attr) { target = ((Attr) target).getOwnerElement(); } while (target != null) { if (target instanceof Element) { addNamespaces(rv, (Element) target); } target = target.getParentNode(); } // Fallback in case no namespace was declared rv.declare(Namespace.create("", "")); return rv; }