XPathExpression xPathExpression = XPathFactory.newInstance().newXPath().compile("//namespace::*");
List<NamespaceNode> nodeList = (ArrayList<NamespaceNode>) xPathExpression.evaluate(new StreamSource(new StringReader(xml)), XPathConstants.NODESET);
Map<String, String> allNamespacesMap = new TreeMap<String, String>();
if (nodeList != null) {
for (int i = 0; i < nodeList.size(); i++) {
NamespaceNode node = nodeList.get(i);
if (!allNamespacesMap.containsKey(node.getLocalPart())) {
allNamespacesMap.put(node.getLocalPart(), node.getStringValue());
}
}
}
return allNamespacesMap;