summaryrefslogtreecommitdiffstats
path: root/docs/html/tutorial-serializer-get-triples.html
blob: b81d5ac3471246f3f133c88808578bf66058535c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!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 Statements (Triples): Raptor RDF Syntax Library Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Raptor RDF Syntax 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.33.1 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</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="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="tutorial-serializing.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="tutorial-serializer-to-destination.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="tutorial-serializer-send-triples.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tutorial-serializer-get-triples"></a>Get or construct RDF Statements (Triples)</h2></div></div></div>
<p>
An <a class="link" href="raptor2-section-triples.html#raptor-statement" title="raptor_statement"><span class="type">raptor_statement</span></a>
containing the triple terms and optional graph term
can be made either by receiving them from a
<a class="link" href="raptor2-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="raptor2-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 statement has three triple terms (subject,
predicate, object) and an optional graph term.  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 statement terms are all instances of
<a class="link" href="raptor2-section-triples.html#raptor-term" title="raptor_term"><span class="type">raptor_term</span></a>
objects constructed with the appropriate constructor for
the URI, blank node or rdf literal types.  The graph term
of the statement is typically a URI or blank node.
</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 &lt;stdio.h&gt;
#include &lt;raptor2.h&gt;
#include &lt;stdlib.h&gt;

/* rdfserialize.c: serialize 1 triple to RDF/XML-Abbrev */

int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_serializer* rdf_serializer = NULL;
  unsigned char *uri_string;
  raptor_uri *base_uri;
  raptor_statement* triple;

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

  rdf_serializer = raptor_new_serializer(world, "rdfxml-abbrev");
  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);
  
  /* Make a triple with URI subject, URI predicate, literal object */
  triple = raptor_new_statement(world);
  triple-&gt;subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://example.org/subject");
  triple-&gt;predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://example.org/predicate");
  triple-&gt;object = raptor_new_term_from_literal(world,
                                                (const unsigned char*)"An example literal",
                                                NULL,
                                                (const unsigned char*)"en");

  /* Write the triple */
  raptor_serializer_serialize_statement(rdf_serializer, triple);

  /* Delete the triple */
  raptor_free_statement(triple);

  raptor_serializer_serialize_end(rdf_serializer);
  raptor_free_serializer(rdf_serializer);
  
  raptor_free_uri(base_uri);
  raptor_free_memory(uri_string);

  raptor_free_world(world);
  return 0;
}

</pre>
<p>Compile it like this:
</p>
<pre class="screen">
$ gcc -o rdfserialize rdfserialize.c `pkg-config raptor2 --cflags --libs`
</pre>
<p>
and run it with an optional base URI argument
</p>
<pre class="screen">
$ ./rdfserialize
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"&gt;
  &lt;rdf:Description rdf:about="http://example.org/subject"&gt;
    &lt;ns0:predicate xmlns:ns0="http://example.org/" xml:lang="en"&gt;An example&lt;/ns0:predicate&gt;
  &lt;/rdf:Description&gt;
&lt;/rdf:RDF&gt;
</pre>
<p>
</p>
</div>
</div>
<br class="example-break">
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.1</div>
</body>
</html>