我在使用 Java 中的 Xpath 提取 XML 节点时遇到问题。这是我正在使用的 YouTube 提要的 URL
http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&start-index=21&max-results=10&v=2
这是提要的摘录
<?xml version="1.0" encoding="UTF-8" ?>
- <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."">
<id>tag:youtube.com,2008:videos</id>
<updated>2013-05-13T13:46:32.464Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
<title>Videos matching: skateboarding dog</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<link rel="alternate" type="text/html" href="http://www.youtube.com" />
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?v=2" />
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/batch?v=2" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&start-index=21&max-results=10&v=2" />
<link rel="service" type="application/atomsvc+xml" href="http://gdata.youtube.com/feeds/api/videos?alt=atom-service&v=2" />
<link rel="previous" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&start-index=11&max-results=10&v=2" />
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos?q=skateboarding+dog&start-index=31&max-results=10&v=2" />
+ <author>
<name>YouTube</name>
<uri>http://www.youtube.com/</uri>
</author>
<generator version="2.1" uri="http://gdata.youtube.com">YouTube data API</generator>
<openSearch:totalResults>277515</openSearch:totalResults>
我创建了一个 NamespaceContext 类
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;
}
}
我用来获取 XML 节点的 Java。我试图获得的节点是,但结果始终为“空”:(
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);