title: how to retrieve XML data using XPath which has a default namespace in Java?
body: <p>I've come across and problem that I've looked up on stack overflow but none of the solutions seems to solve the problem for me.</p>
  
  <p>I'm retrieving XML data from Yahoo and it comes back as below (truncated for brevity's sake).</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;fantasy_content xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" copyright="Data provided by Yahoo! and STATS, LLC" refresh_rate="31" time="55.814027786255ms" xml:lang="en-US" yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/league/328.l.108462/settings"&gt;
      &lt;league&gt;
          &lt;league_key&gt;328.l.108462&lt;/league_key&gt;
          &lt;league_id&gt;108462&lt;/league_id&gt;
          &lt;draft_status&gt;postdraft&lt;/draft_status&gt;
      &lt;/league&gt;
  &lt;/fantasy_content&gt;
  </code></pre>
  
  <p>I've been having a problem getting XPath to retrieve any elements so I've written a unit test to try to resolve it and it looks like:</p>
  
  <pre><code>    final File file = new File("league-settings.xml");
      javax.xml.parsers.DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      dbFactory.setNamespaceAware(true);
      javax.xml.parsers.DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      org.w3c.dom.Document doc = dBuilder.parse(file);
      javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
      xPath.setNamespaceContext(new YahooNamespaceContext());
      final String expression = "yfs:league";
      final XPathExpression expr = xPath.compile(expression);
      Object nodes = expr.evaluate(doc, XPathConstants.NODESET);
  
      assert(nodes instanceof NodeList);
      NodeList leagueNodes = (NodeList)nodes;
      int leaguesLength = leagueNodes.getLength();
      assertEquals(leaguesLength, 1);
  </code></pre>
  
  <p>The YahooNamespaceContext class I created to map the namespaces looks as follows:</p>
  
  <pre><code>public class YahooNamespaceContext implements NamespaceContext {
      public static final String YAHOO_NS = "http://www.yahooapis.com/v1/base.rng";
      public static final String DEFAULT_NS = "http://fantasysports.yahooapis.com/fantasy/v2/base.rng";
      public static final String YAHOO_PREFIX = "yahoo";
      public static final String DEFAULT_PREFIX = "yfs";
  
      private final Map&lt;String, String&gt; namespaceMap = new HashMap&lt;String, String&gt;();
      public YahooNamespaceContext() {
          namespaceMap.put(DEFAULT_PREFIX, DEFAULT_NS);
          namespaceMap.put(YAHOO_PREFIX, YAHOO_NS);
      }
  
      public String getNamespaceURI(String prefix) {
          return namespaceMap.get(prefix);
      }
  
      public String getPrefix(String uri) {
          throw new UnsupportedOperationException();
      }
  
      public Iterator&lt;String&gt; getPrefixes(String uri) {
          throw new UnsupportedOperationException();
      }
  
  }
  </code></pre>
  
  <p>Any help with people with more experience with XML namespaces or debugging tips into Xpath compilation/evaluation would be appreciated.</p>