1

I'm parsing an XML feed that looks something like this:

<?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>

One thing I wanted to do was get all the namespaces by URL and get the associated prefix for later parsing. For example, if I know the itunes DTD is located at: http://www.itunes.com/dtds/podcast-1.0.dtd I want to get the associated prefix. Here is some code I want to use to do that:

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;
    }
 }

However, the only attribute that shows up here is the rss "version" attribute (getAttributeCount returns 1 and the name and values for "version" and "2.0"). Is there some way to get the namespaces?

IcedDante
  • 6,145
  • 12
  • 57
  • 100

0 Answers0