private static void parseNamespacesForElement ( XmlPullParser parser , XmlNamespaceDictionary namespaceDictionary ) throws XmlPullParserException { int eventType = parser . getEventType ( ) ; Preconditions . checkState ( eventType == XmlPullParser . START_TAG , "expected start of XML element, but got something else (event type %s)" , eventType ) ; int depth = parser . getDepth ( ) ; int nsStart = parser . getNamespaceCount ( depth - 1 ) ; int nsEnd = parser . getNamespaceCount ( depth ) ; for ( int i = nsStart ; i < nsEnd ; i ++ ) { String namespace = parser . getNamespaceUri ( i ) ; // if namespace isn't already in our dictionary, add it now if ( namespaceDictionary . getAliasForUri ( namespace ) == null ) { String prefix = parser . getNamespacePrefix ( i ) ; String originalAlias = prefix == null ? "" : prefix ; // find an available alias String alias = originalAlias ; int suffix = 1 ; while ( namespaceDictionary . getUriForAlias ( alias ) != null ) { suffix ++ ; alias = originalAlias + suffix ; } namespaceDictionary . set ( alias , namespace ) ; } } }
Parses the namespaces declared on the current element into the namespace dictionary .