indi
indi

Reputation: 63

Android SAX Parser for RDF feed

I'm completely stuck for a couple of days on trying to parse an RDF feed (newbie on Android parsers). In spite of using:

Element item = root.getChild("", ITEM);

since my items come from the root instead of CHANNEL, as appointed here. Anyway, I'm only retrieving 0 messages

<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
     xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://...">
    <title>Notícies</title>
    <link>...</link>
    <description></description>
        <syn:updatePeriod>daily</syn:updatePeriod>
        <syn:updateFrequency>1</syn:updateFrequency>
        <syn:updateBase>2009-08-25T16:01:24Z</syn:updateBase>
        <image rdf:resource="..."/>
        <items>
            <rdf:Seq>
            <rdf:li rdf:resource="..."/>
            <rdf:li rdf:resource="..."/>
            </rdf:Seq>
        </items>
        </channel>

<item rdf:about="...">        
   <title>...</title>        
   <link>....</link>        
   <description>...</description>        
   <dc:publisher>No publisher</dc:publisher>
   <dc:creator>administrador</dc:creator>
   <dc:rights></dc:rights>                
   <dc:date>2013-03-22T16:18:36Z</dc:date>        
   <dc:type>Noticia</dc:type>    
</item>
<item rdf:about="...">        
   <title>...</title>        
   <link>....</link>        
   <description>...</description>        
   <dc:publisher>No publisher</dc:publisher>
   <dc:creator>administrador</dc:creator>
   <dc:rights></dc:rights>                
   <dc:date>2013-03-22T16:18:36Z</dc:date>        
   <dc:type>Noticia</dc:type>    
</item>
<item rdf:about="...">        
   <title>...</title>        
   <link>....</link>        
   <description>...</description>        
   <dc:publisher>No publisher</dc:publisher>
   <dc:creator>administrador</dc:creator>
   <dc:rights></dc:rights>                
   <dc:date>2013-03-22T16:18:36Z</dc:date>        
   <dc:type>Noticia</dc:type>    
</item>
 ...

</rdf:RDF>

Using my SAX Parser as follows:

public class AndroidSaxFeedParser extends BaseFeedParser {


    static final String RDF_NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
    static final String RSS_NAMESPACE = "";

    static final String RSS = "RDF";
    public AndroidSaxFeedParser(String feedUrl) {
        super(feedUrl);
    }

    public List<Message> parse() {
        final Message currentMessage = new Message();
        //RootElement root = new RootElement(RSS);
        RootElement root = new RootElement(RDF_NAMESPACE, RSS);
        final List<Message> messages = new ArrayList<Message>();

        //Element channel = root.getChild(CHANNEL);
        //Element channel = root.getChild(RDF_NAMESPACE, CHANNEL);
        Log.e("Valor Root", root.toString());

        Element item = root.getChild("", ITEM);
        //Element item = channel.getChild(RDF_NAMESPACE,ITEM);
        item.setEndElementListener(new EndElementListener(){
            public void end() {
                messages.add(currentMessage.copy());
            }
        });
        item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setTitle(body);
            }
        });
        item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setLink(body);
            }
        });
        item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setDescription(body);
            }
        });
        item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setDate(body);
            }
        });
        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return messages;
    }
   }

I've tried

Element item = root.getChild("RDF_NAMESPACE", ITEM); Element item = root.getChild("", ITEM); Element item = root.getChild(ITEM);

But those 3 options return 0 messages. Any help would be really much appreciated. Many thanks in advance.

Upvotes: 0

Views: 667

Answers (1)

Norman Gray
Norman Gray

Reputation: 12514

I can't help with your specific question, but my general advice would be: don't do this – don't try to parse RDF as XML.

That particular RDF syntax, RDF/XML, is indeed syntactically XML, but it's the result of cramming a graph model (RDF) into a tree model (XML) and then serialising that. Parsing RDF/XML has all sorts of alternatives and edge-cases, and is a general nightmare; it'll have you tearing your hair out.

It looks like you're doing this in Java. MRG is a good, lightweight, Java RDF parser, which I've used successfully in the past.

Edited: It seems that a similar question has been asked before (asking specifically for RSS tools rather than XML ones). See How to write an RSS feed with Java? . If the only RDF you're likely to parse is RSS, then that looks like a good bet, but I'd evaluate both, if I were you, just to get a fuller sense of the possibilities.

Also, separately, you seem to have two rdf:RDF elements in your example. Is that a cut-and-paste error, or are you dealing with badly malformed RDF?

Upvotes: 1

Related Questions