5

I recently downloaded a ZIP file in RDF format from the Barcelona Open Data web page.

This format is a subset (or that is what I understand) of XML. I’ve been trying to parse it with an objective C API (NSXMLParser), but all my efforts have brought no happy ending. I have searched Stack Overflow for hours and nobody appears to have any info into how to work with this type of data.

Anybody here has any experience working with RDF formats? Any tips or guidelines?

5

RDF has many different serializations (or formats); there is no "RDF format".

As you say it’s XML, it’s most likely the RDF/XML serialization.

You should not use a usual XML parser to work with it; you’ll want to work on the RDF triple level instead.

So you need an RDF-aware tool that can input RDF/XML; or you can convert your file from RDF/XML to a more human-readable format like Turtle, and use a tool that inputs this serialization.

To query the data, you’ll want to use SPARQL.

There are many tools (Redland librdf (C), Jena (Java), …), and I can’t give a recommendation, but the sister site Software Recommendations might help.

See also a list of tools in W3C’s Semantic Web wiki (also with links to other off-site lists).

3

I have experience of working with RDF in Python. I don't know if it is suitable for you. In specific, I have used the library RDFlib.

from rdflib import Graph

g = Graph()
g.parse("http://bigasterisk.com/foaf.rdf")

From now on, you have two options. Query the graph with SPARQL or parse the file with triples.

With SPARQL:

qres = g.query(
    """SELECT DISTINCT ?aname ?bname
       WHERE {
          ?a foaf:knows ?b .
          ?a foaf:name ?aname .
          ?b foaf:name ?bname .
       }""")

for row in qres:
    print("%s knows %s" % row)

With simple parsing you will iterate through triples.

for s, p, o in g:
    print((s, p, o))

Try to run this code in a small RDF file in order to understand how it works and what exactly represent each of s,p and o. If you still have a problem, let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.