| Raptor RDF Syntax Parsing and Serializing Library Manual |
|---|
An raptor_statement can be made either by receiving them from a raptor_parser via parsing or can be constructed by hand.
When constructing by hand, the raptor_statement structure should be allocated by the application and the fields filled in. Each triple has three parts. The subject can be a URI or blank node, the predicate can only be a URI and the object can be a URI, blank node or RDF literal. RDF literals can have either just a Unicode string, a Unicode string and a language or a Unicode string and a datatype URI.
The triple part types are set as fields named
like subject_type for describing
field subject.
So to initialise the subject of the triple,
set the field statement.subject to point to a
previously allocated
raptor_uri* object (for URI)
or char* (for blank node) and
set statement.subject_type
to RAPTOR_IDENTIFIER_TYPE_RESOURCE or
RAPTOR_IDENTIFIER_TYPE_ANONYMOUS respectively.
Triple predicates are always of type
RAPTOR_IDENTIFIER_TYPE_RESOURCE.
Triple objects are all all types given above and also
RAPTOR_IDENTIFIER_TYPE_LITERAL which takes
an unsigned char* pointer plus an optional
language char* pointer
in the object_literal_language field OR a
a raptor_uri* literal datatype pointer
in the object_literal_datatype field.
The triple part types are described under
raptor_identifier_type.
Example 3. rdfserialize.c: Serialize 1 triple to RDF/XML (Abbreviated)
#include <stdio.h>
#include <raptor.h>
#include <stdlib.h>
/* rdfserialize.c: serialize 1 triple to RDF/XML-Abbrev */
int
main(int argc, char *argv[])
{
raptor_serializer* rdf_serializer=NULL;
unsigned char *uri_string;
raptor_uri *base_uri;
raptor_statement* triple;
raptor_init();
uri_string=raptor_uri_filename_to_uri_string(argv[1]);
base_uri=raptor_new_uri(uri_string);
rdf_serializer=raptor_new_serializer("rdfxml-abbrev");
raptor_serialize_start_to_file_handle(rdf_serializer, base_uri, stdout);
/* Make a triple with URI subject, URI predicate, literal object */
triple=(raptor_statement*)malloc(sizeof(raptor_statement));
triple->subject=(void*)raptor_new_uri((const unsigned char*)"http://example.org/subject");
triple->subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
triple->predicate=(void*)raptor_new_uri((const unsigned char*)"http://example.org/predicate");
triple->predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
triple->object="An example literal";
triple->object_type=RAPTOR_IDENTIFIER_TYPE_LITERAL;
triple->object_literal_language=(const unsigned char*)"en";
/* Write the triple */
raptor_serialize_statement(rdf_serializer, triple);
/* Delete the triple */
raptor_free_uri((raptor_uri*)triple->subject);
raptor_free_uri((raptor_uri*)triple->predicate);
free(triple);
raptor_serialize_end(rdf_serializer);
raptor_free_serializer(rdf_serializer);
raptor_free_uri(base_uri);
raptor_free_memory(uri_string);
raptor_finish();
return 0;
}
Compile it like this:
$ gcc -o rdfserialize rdfserialize.c `raptor-config --cflags` `raptor-config --libs`
and run it with an optional base URI argument
$ ./rdfserialize
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.org/subject">
<ns0:predicate xmlns:ns0="http://example.org/" xml:lang="en">An example</ns0:predicate>
</rdf:Description>
</rdf:RDF>