The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Serializing example code</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.71.0">
<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-destroy.html" title="Destroy the serializer">
<link rel="next" href="reference-manual.html" title="Part&#160;II.&#160;Raptor Reference Manual">
<meta name="generator" content="GTK-Doc V1.7 (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&#160;I.&#160;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&#160;II.&#160;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-destroy.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="reference-manual.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-example"></a>Serializing example code</h2></div></div></div>
<div class="example">
<a name="raptor-example-rdfcat"></a><p class="title"><b>Example&#160;4.&#160;<code class="filename">rdfcat.c</code>: Read any RDF syntax and serialize to RDF/XML (Abbreviated)</b></p>
<div class="example-contents">
<pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;raptor.h&gt;

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

static raptor_serializer* rdf_serializer;

void
serialize_triple(void* user_data, const raptor_statement* triple) 
{
  raptor_serialize_statement(rdf_serializer, triple);
}

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

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

  raptor_init();

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

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

  rdf_serializer=raptor_new_serializer("rdfxml-abbrev");

  raptor_serialize_start_to_file_handle(rdf_serializer, base_uri, stdout);
  raptor_parse_file(rdf_parser, uri, base_uri);
  raptor_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_finish();
}

</pre>
<p>Compile it like this:
</p>
<pre class="screen">
$ gcc -o rdfcat rdfcat.c `raptor-config --cflags` `raptor-config --libs`
</pre>
<p>
and run it on an RDF file as:
</p>
<pre class="screen">
$ ./rdfcat raptor.rdf
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://usefulinc.com/ns/doap#"&gt;
  &lt;rdf:Description rdf:about=""&gt;
    &lt;foaf:maker&gt;
      &lt;foaf:Person&gt;
        &lt;foaf:name&gt;Dave Beckett&lt;/foaf:name&gt;
...
</pre>
<p>
</p>
</div>
</div>
<br class="example-break">
</div>
</body>
</html>