title: Get all namespaces in xml document java
body: <p>I want to get all the namespaces in the xml file for example</p>
  
  <p>[<a href="http://www.utilities-online.info/xpath/?save=a4ac7360-1994-44c2-9c26-c9f741dc2e44-xpath#.VeRWSvmqpBc][1]" rel="nofollow noreferrer">http://www.utilities-online.info/xpath/?save=a4ac7360-1994-44c2-9c26-c9f741dc2e44-xpath#.VeRWSvmqpBc][1]</a></p>
  
  <p>basically problem is to to search using xpath based on namespaces and for that purpose i have to use <code>setNamespaceContext</code> and don't know namespaces value because it will be on runtime while parsing file. </p>
  
  <pre><code>xpath.setNamespaceContext(namespaces);
  
  public class SimpleNamespaceContext implements NamespaceContext {
  
      private final Map&lt;String, String&gt; PREF_MAP = new HashMap&lt;String, String&gt;();
  
      public SimpleNamespaceContext(final Map&lt;String, String&gt; prefMap) {
          PREF_MAP.putAll(prefMap);       
      }
  
      public String getNamespaceURI(String prefix) {
          return PREF_MAP.get(prefix);
      }
  
      public String getPrefix(String uri) {
          throw new UnsupportedOperationException();
      }
  
      public Iterator getPrefixes(String uri) {
          throw new UnsupportedOperationException();
      }
  
  }
  XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  HashMap&lt;String, String&gt; prefMap = new HashMap&lt;String, String&gt;() {{
      put("main", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
      put("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  }};
  SimpleNamespaceContext namespaces = new SimpleNamespaceContext(prefMap);
  xpath.setNamespaceContext(namespaces);
  XPathExpression expr = xpath
          .compile("/main:workbook/main:sheets/main:sheet[1]");
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  </code></pre>
  
  <p>reference link [<a href="https://stackoverflow.com/questions/6390339/how-to-query-xml-using-namespaces-in-java-with-xpath][1]">How to query XML using namespaces in Java with XPath?</a></p>
  
  <p>To implement above mention reference i need to get all namespaces 
  query i am using to get all namespaces using xpath is</p>
  
  <pre><code>    try {
  XPathFactory xPathfactory = XPathFactory.newInstance();
  XPath xpath = xPathfactory.newXPath();
  XPathExpression expr = xpath.compile("//namespace::*");
  NodeList a = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 
  System.out.println(a.getLength());
  } catch (Exception e) {
  Utility.consoleInfo(e);
  }
  </code></pre>
  
  <p>but this always return nodelist of size 0 .
  Am i going in wrong direction to achieve this? or what else would be the solution to this problem ?</p>
  
  <p><strong>Summary</strong> 
  How should i get all namespaces so that it can be used in xpath ?</p>
  
  <p>please help me in this regard </p>