<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Get or construct RDF Triples</title> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2"> <link rel="start" href="index.html" title="Raptor RDF Syntax Parsing and Serializing Library Manual"> <link rel="up" href="tutorial-serializing.html" title="Serializing RDF triples to a syntax"> <link rel="prev" href="tutorial-serializer-to-destination.html" title="Provide a destination for the serialized syntax"> <link rel="next" href="tutorial-serializer-send-triples.html" title="Send RDF Triples to serializer"> <meta name="generator" content="GTK-Doc V1.10 (XML mode)"> <link rel="stylesheet" href="style.css" type="text/css"> <link rel="chapter" href="introduction.html" title="Raptor Overview"> <link rel="part" href="tutorial.html" title="Part I. Raptor Tutorial"> <link rel="chapter" href="tutorial-initialising-finishing.html" title="Initialising and Finishing using the Library"> <link rel="chapter" href="tutorial-querying-functionality.html" title="Listing built-in functionality"> <link rel="chapter" href="tutorial-parsing.html" title="Parsing syntaxes to RDF Triples"> <link rel="chapter" href="tutorial-serializing.html" title="Serializing RDF triples to a syntax"> <link rel="part" href="reference-manual.html" title="Part II. Raptor Reference Manual"> <link rel="chapter" href="raptor-parsers.html" title="Parsers in Raptor (syntax to triples)"> <link rel="chapter" href="raptor-serializers.html" title="Serializers in Raptor (triples to syntax)"> <link rel="index" href="ix01.html" title="Index"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"> <td><a accesskey="p" href="tutorial-serializer-to-destination.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td> <td><a accesskey="u" href="tutorial-serializing.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td> <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td> <th width="100%" align="center">Raptor RDF Syntax Parsing and Serializing Library Manual</th> <td><a accesskey="n" href="tutorial-serializer-send-triples.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td> </tr></table> <div class="section" lang="en"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="tutorial-serializer-get-triples"></a>Get or construct RDF Triples</h2></div></div></div> <p> An <a class="link" href="raptor-section-triples.html#raptor-statement" title="raptor_statement"><span class="type">raptor_statement</span></a> can be made either by receiving them from a <a class="link" href="raptor-section-parser.html#raptor-parser" title="raptor_parser"><span class="type">raptor_parser</span></a> via parsing or can be constructed by hand.</p> <p>When constructing by hand, the <a class="link" href="raptor-section-triples.html#raptor-statement" title="raptor_statement"><span class="type">raptor_statement</span></a> 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.</p> <p>The triple part types are set as fields named like <code class="literal">subject_type</code> for describing field <code class="literal">subject</code>. So to initialise the subject of the triple, set the field <code class="literal">statement.subject</code> to point to a previously allocated <a class="link" href="raptor-section-uri.html#raptor-uri" title="raptor_uri"><span class="type">raptor_uri*</span></a> object (for URI) or <code class="literal">char*</code> (for blank node) and set <code class="literal">statement.subject_type</code> to <code class="literal">RAPTOR_IDENTIFIER_TYPE_RESOURCE</code> or <code class="literal">RAPTOR_IDENTIFIER_TYPE_ANONYMOUS</code> respectively. Triple predicates are always of type <code class="literal">RAPTOR_IDENTIFIER_TYPE_RESOURCE</code>. Triple objects are all all types given above and also <code class="literal">RAPTOR_IDENTIFIER_TYPE_LITERAL</code> which takes an <code class="literal">unsigned char*</code> pointer plus an optional language <code class="literal">char*</code> pointer in the <code class="literal">object_literal_language</code> field OR a a <code class="literal">raptor_uri*</code> literal datatype pointer in the <code class="literal">object_literal_datatype</code> field. The triple part types are described under <a class="link" href="raptor-section-triples.html#raptor-identifier-type" title="enum raptor_identifier_type"><span class="type">raptor_identifier_type</span>.</a> </p> <div class="example"> <a name="raptor-example-rdfserialize"></a><p class="title"><b>Example 3. <code class="filename">rdfserialize.c</code>: Serialize 1 triple to RDF/XML (Abbreviated)</b></p> <div class="example-contents"> <pre class="programlisting"> #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; } </pre> <p>Compile it like this: </p> <pre class="screen"> $ gcc -o rdfserialize rdfserialize.c `raptor-config --cflags` `raptor-config --libs` </pre> <p> and run it with an optional base URI argument </p> <pre class="screen"> $ ./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> </pre> <p> </p> </div> </div> <br class="example-break"> </div> <div class="footer"> <hr> Generated by GTK-Doc V1.10</div> </body> </html>