2

I've been trying for a while to parse some RDF/XML files with the raptor2 C library. This example code will read an NTriples file and print it, but not RDF/XML.

From the raptor tutorial:

#include <stdio.h>
#include <raptor2.h>

/* rdfcat.c: parse any RDF syntax and serialize to RDF/XML-Abbrev */

static raptor_serializer* rdf_serializer;

static void
serialize_triple(void* user_data, raptor_statement* triple) 
{
  raptor_serializer_serialize_statement(rdf_serializer, triple);
}

static void
declare_namespace(void* user_data, raptor_namespace *nspace)
{
  raptor_serializer_set_namespace_from_namespace(rdf_serializer, nspace);
}

int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri, *base_uri;

  world = raptor_new_world();

  uri_string = raptor_uri_filename_to_uri_string(argv[1]);
  uri = raptor_new_uri(world, uri_string);
  base_uri = raptor_uri_copy(uri);

  /* Ask raptor to work out which parser to use */
  rdf_parser = raptor_new_parser(world, "guess");
  raptor_parser_set_statement_handler(rdf_parser, NULL, serialize_triple);
  raptor_parser_set_namespace_handler(rdf_parser, NULL, declare_namespace);

  rdf_serializer = raptor_new_serializer(world, "rdfxml-abbrev");

  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);
  raptor_parser_parse_file(rdf_parser, uri, base_uri);
  raptor_serializer_serialize_end(rdf_serializer);

  raptor_free_serializer(rdf_serializer);
  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  raptor_free_memory(uri_string);

  raptor_free_world(world);

  return 0;
}

Here is an example from Wikipedia of the same RDF written in both formats. The first version prints (with a couple errors, but I don't think that's important), but the second one doesn't.

NTriples:

<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Document> .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://purl.org/dc/terms/title> "N-Triples"@en-US .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://xmlns.com/foaf/0.1/maker> _:art .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://xmlns.com/foaf/0.1/maker> _:dave .

_:art <http://www.w3.org/1999/02/22-rdf-syntax-ns#> <http://xmlns.com/foaf/0.1/Person> .
_:art <http://xmlns.com/foaf/0.1/name> "Art Barstow".

_:dave <http://www.w3.org/1999/02/22-rdf-syntax-ns#> <http://xmlns.com/foaf/0.1/Person> .
_:dave <http://xmlns.com/foaf/0.1/name> "Dave Beckett".

RDFXML:

<rdf:RDF 
    xmlns="http://xmlns.com/foaf/0.1/"
    xmlns:dc="http://purl.org/dc/terms/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

    <Document rdf:about="http://www.w3.org/2001/sw/RDFCore/ntriples/">
        <dc:title xml:lang="en-US">N-Triples</dc:title>
        <maker>
            <Person rdf:nodeID="art">
                <name>Art Barstow</name>
            </Person>
        </maker>
        <maker>
            <Person rdf:nodeID="dave">
                <name>Dave Beckett</name>
            </Person>
        </maker>
    </Document>
</rdf:RDF>

Any ideas why? Thanks!

EDIT: The RDFXML one should be valid because it passes the W3C RDF Validator.

EDIT: Explicitly setting the parser to "rdfxml" doesn't help. I actually just found out about the guess option from this example and was excited because before I had been manually checking the extension and calling it with "ntriples" or "rdfxml".

laalto
  • 150,114
  • 66
  • 286
  • 303
jefdaj
  • 2,025
  • 2
  • 21
  • 33

1 Answers1

3

It's still just guessing what the input format is - the parser is "guess":

  /* Ask raptor to work out which parser to use */

Guesses can be wrong. Set the parser explicitly to "ntriples" or "rdfxml" and it won't guess, it'll parse exactly what you give it.

dajobe
  • 4,938
  • 35
  • 41
  • Thanks that would make total sense, but even when it's set to "rdfxml" nothing comes out. It will read the ntriples example either way though, with "ntriples" or "guess". – jefdaj Mar 11 '12 at 18:42
  • Your code and your rdfxml example work for me. It must be something on your machine. Maybe you are linking against a different raptor. – dajobe Mar 12 '12 at 20:10
  • I discovered my project does work on Windows, so it probably is something about the machine. I'll post back here once I figure out what exactly. Thanks! – jefdaj Mar 13 '12 at 02:23
  • Well I ended up dropping raptor and just using libxml--not because there was anything wrong with raptor, but because I needed to write a custom xml format anyway and it seemed simpler to stick with the same library throughout. I'm marking this correct because I'm pretty sure it was a problem somewhere in my code rather than the example, and because not relying on the parser to choose formats is good advice. :) – jefdaj Mar 29 '12 at 17:21
  • @dajobe I am new to using raptor libraries. Is there a way to use this library for a number of files (uri will be a number of files in buffer) in a Java project in Eclipse? If not, how do i write the output of parsing into a file instead of printing it on console ? – Prathamesh dhanawade Oct 19 '17 at 17:14