public static Map < String , String > lookupNamespaces ( String xml ) { Map < String , String > namespaces = new HashMap < String , String > ( ) ; //TODO: handle inner CDATA sections because namespaces they might interfere with real namespaces in xml fragment if ( xml . indexOf ( XMLConstants . XMLNS_ATTRIBUTE ) != - 1 ) { String [ ] tokens = StringUtils . split ( xml , XMLConstants . XMLNS_ATTRIBUTE ) ; do { String token = tokens [ 1 ] ; String nsPrefix ; if ( token . startsWith ( ":" ) ) { nsPrefix = token . substring ( 1 , token . indexOf ( ' ' ) ) ; } else if ( token . startsWith ( "=" ) ) { nsPrefix = XMLConstants . DEFAULT_NS_PREFIX ; } else { //we have found a "xmlns" phrase that is no namespace attribute - ignore and continue tokens = StringUtils . split ( token , XMLConstants . XMLNS_ATTRIBUTE ) ; continue ; } String nsUri ; try { nsUri = token . substring ( token . indexOf ( ' ' ) + 1 , token . indexOf ( ' ' , token . indexOf ( ' ' ) + 1 ) ) ; } catch ( StringIndexOutOfBoundsException e ) { //maybe we have more luck with single "'" nsUri = token . substring ( token . indexOf ( ' ' ) + 1 , token . indexOf ( ' ' , token . indexOf ( ' ' ) + 1 ) ) ; } namespaces . put ( nsPrefix , nsUri ) ; tokens = StringUtils . split ( token , XMLConstants . XMLNS_ATTRIBUTE ) ; } while ( tokens != null ) ; } return namespaces ; }
Look up namespace attribute declarations in the XML fragment and store them in a binding map where the key is the namespace prefix and the value is the namespace uri .