You want to dynamically extract the namespaces URIs and the prefixes from the XML? That's a genuinely bad idea.
XML namespaces are part of the contract between your application code and the XML it processes. They ought to be hard-coded into your application.
The reason is simple. These XMLs look different but are all the same document:
<foo:root xmlns:foo="http://main/ns" xmlns:bar="http://secondary/ns">
<foo:child bar:attr="1234">some data</foo:child>
</foo:root>
or
<bar:root xmlns:bar="http://main/ns" xmlns:foo="http://secondary/ns">
<bar:child foo:attr="1234">some data</bar:child>
</bar:root>
or
<root xmlns="http://main/ns" xmlns:baz="http://secondary/ns">
<child baz:attr="1234">some data</child>
</root>
So if you extract namespaces dynamically from them, your subsequent code will inevitably (and unnecessarily) break.
Use actual namespace URIs in your code and choose prefixes to your liking - prefixes are ephemeral, they don't have to match up with the XML files. XML is strongly typed data, treat it accordingly.
In other words, namespace prefixes are aliases, a convenience facility, they only exist in serialized data. They exist in XML, they don't exist in the DOM. They exist in your application's XPath expressions, they don't exist in the abstract tree the XPath expressions are parsed into. Those are two completely separate domains. If the same prefixes are used in both domains, that's entirely coincidental. Don't build application logic that transfers prefixes from one domain to the other, as this is bound to break.