我正在解析看起来像这样的XML Feed:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cc="http://web.resource.org/cc/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
...
</channel>
</rss>
我想要做的一件事是通过URL获取所有命名空间并获取相关的前缀以便以后解析。例如,如果我知道itunes DTD位于:http://www.itunes.com/dtds/podcast-1.0.dtd我想获得相关的前缀。这是我想用来做的一些代码:
if(parser.getEventType() != START_TAG && !"rss".equalsIgnoreCase(parser.getName())) {
throw new IllegalStateException();
}
for(int i=0; i<parser.getAttributeCount(); i++) {
if(!"xmlns".equalsIgnoreCase(parser.getAttributePrefix(i))) {
continue;
}
String namespace = parser.getAttributeName(i);
String namespaceUrl = parser.getAttributeValue(i);
if(namespaceUrl.contains("www.itunes.com")) {
ITUNES_NAMESPACE = namespace;
} else if(namespaceUrl.contains("www.w3.org/2005/Atom")) {
ATOM_NAMESPACE = namespace;
}
}
然而,这里显示的唯一属性是rss&#34;版本&#34; attribute(getAttributeCount返回1,&#34的名称和值;版本&#34;和&#34; 2.0&#34;)。有没有办法获得命名空间?