Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Xpath to get XML in Java trouble with namespaces
    primarykey
    data
    text
    <p>I'm having trouble extracting XML nodes using Xpath in Java. Here is the URL for the YouTube feed I am using</p> <p><a href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&amp;start-index=21&amp;max-results=10&amp;v=2" rel="nofollow">http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&amp;start-index=21&amp;max-results=10&amp;v=2</a></p> <p>Here's an extract of the feed</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; - &lt;feed xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/"CEANQ3o9fCp7I2A9WhBbFEg.""&gt; &lt;id&gt;tag:youtube.com,2008:videos&lt;/id&gt; &lt;updated&gt;2013-05-13T13:46:32.464Z&lt;/updated&gt; &lt;category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" /&gt; &lt;title&gt;Videos matching: skateboarding dog&lt;/title&gt; &lt;logo&gt;http://www.youtube.com/img/pic_youtubelogo_123x63.gif&lt;/logo&gt; &lt;link rel="alternate" type="text/html" href="http://www.youtube.com" /&gt; &lt;link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?v=2" /&gt; &lt;link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/batch?v=2" /&gt; &lt;link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&amp;start-index=21&amp;max-results=10&amp;v=2" /&gt; &lt;link rel="service" type="application/atomsvc+xml" href="http://gdata.youtube.com/feeds/api/videos?alt=atom-service&amp;v=2" /&gt; &lt;link rel="previous" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&amp;start-index=11&amp;max-results=10&amp;v=2" /&gt; &lt;link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&amp;start-index=31&amp;max-results=10&amp;v=2" /&gt; + &lt;author&gt; &lt;name&gt;YouTube&lt;/name&gt; &lt;uri&gt;http://www.youtube.com/&lt;/uri&gt; &lt;/author&gt; &lt;generator version="2.1" uri="http://gdata.youtube.com"&gt;YouTube data API&lt;/generator&gt; &lt;openSearch:totalResults&gt;277515&lt;/openSearch:totalResults&gt; </code></pre> <p>I have created a NamespaceContext class</p> <pre><code>import java.util.Iterator; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; public class YouTubeNameSpaceContext implements NamespaceContext { @Override public String getNamespaceURI(String prefix) { if (prefix == null) throw new NullPointerException("Invalid Namespace Prefix"); else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) return "http://www.w3.org/2005/Atom"; else if ("app".equals(prefix)) return "http://www.w3.org/2007/app"; else if ("media".equals(prefix)) return "http://search.yahoo.com/mrss/"; else if ("openSearch".equals(prefix)) return "http://a9.com/-/spec/opensearch/1.1/"; else if ("gd".equals(prefix)) return "http://schemas.google.com/g/2005"; else if ("yt".equals(prefix)) return "http://gdata.youtube.com/schemas/2007"; else return XMLConstants.NULL_NS_URI; } @Override public String getPrefix(String arg0) { // TODO Auto-generated method stub return null; } @Override public Iterator getPrefixes(String arg0) { // TODO Auto-generated method stub return null; } </code></pre> <p>}</p> <p>The Java which I'm using to get the XML node. THe node I'm trying to get is but the result is always 'null' :( </p> <pre><code>XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new YouTubeNameSpaceContext()); String QUERY_FORM_NUMBER = "//feed/openSearch:totalResults"; Node result = (Node)xPath.evaluate(QUERY_FORM_NUMBER, document, XPathConstants.NODE); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload