public static Map < String , String > lookupNamespaces ( Node referenceNode ) { Map < String , String > namespaces = new HashMap < String , String > ( ) ; Node node ; if ( referenceNode . getNodeType ( ) == Node . DOCUMENT_NODE ) { node = referenceNode . getFirstChild ( ) ; } else { node = referenceNode ; } if ( node != null && node . hasAttributes ( ) ) { for ( int i = 0 ; i < node . getAttributes ( ) . getLength ( ) ; i ++ ) { Node attribute = node . getAttributes ( ) . item ( i ) ; if ( attribute . getNodeName ( ) . startsWith ( XMLConstants . XMLNS_ATTRIBUTE + ":" ) ) { namespaces . put ( attribute . getNodeName ( ) . substring ( ( XMLConstants . XMLNS_ATTRIBUTE + ":" ) . length ( ) ) , attribute . getNodeValue ( ) ) ; } else if ( attribute . getNodeName ( ) . startsWith ( XMLConstants . XMLNS_ATTRIBUTE ) ) { //default namespace namespaces . put ( XMLConstants . DEFAULT_NS_PREFIX , attribute . getNodeValue ( ) ) ; } } } return namespaces ; }
Look up namespace attribute declarations in the specified node and store them in a binding map where the key is the namespace prefix and the value is the namespace uri .