C++ 使用Raptor RDF解析器工具包生成FOAF rdfxml文件
我想使用编写一个C/C++程序来生成以下输出(选中): 只是为了记录,我正在使用。 我想出了以下代码:C++ 使用Raptor RDF解析器工具包生成FOAF rdfxml文件,c++,rdf,linked-data,foaf,redland,C++,Rdf,Linked Data,Foaf,Redland,我想使用编写一个C/C++程序来生成以下输出(选中): 只是为了记录,我正在使用。 我想出了以下代码: #include "raptor2/raptor2.h" int main(int argc, char* argv[]) { FILE* outfile = fopen("myTestfile.rdf", "w"); raptor_world* world = raptor_new_world(); rdf_serializer = raptor_new_seri
#include "raptor2/raptor2.h"
int main(int argc, char* argv[]) {
FILE* outfile = fopen("myTestfile.rdf", "w");
raptor_world* world = raptor_new_world();
rdf_serializer = raptor_new_serializer(world, "rdfxml" /* "turtle" */);
raptor_serializer_start_to_file_handle(rdf_serializer, nullptr, outfile);
const unsigned char* prefix = (const unsigned char*)"foaf";
raptor_uri* uri = raptor_new_uri(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/");
raptor_serializer_set_namespace(rdf_serializer, uri, prefix);
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"genid:A4486");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"http://xmlns.com/foaf/0.1/Person", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/Person");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/name");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"Jimmy Wales", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/Person");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/mbox");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"mailto:jwales@bomis.com", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
raptor_serializer_serialize_end(rdf_serializer);
raptor_free_serializer(rdf_serializer);
raptor_free_world(world);
fclose(outfile);
}
生成的文件如下所示:
Number Subject Predicate Object
1 genid:A4486 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
2 genid:A4486 http://xmlns.com/foaf/0.1/name "Jimmy Wales"@en
3 genid:A4486 http://xmlns.com/foaf/0.1/mbox mailto:jwales@bomis.com
4 genid:A4486 http://xmlns.com/foaf/0.1/nick "Jimbo"@en
5 genid:A4486 http://xmlns.com/foaf/0.1/depiction http://upload.wikimedia.org/wikipedia/commons/1/19/Jimbo_Wales_in_France_cropped.jpg
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="genid:A4486">
<rdf:type>http://xmlns.com/foaf/0.1/Person</rdf:type>
</rdf:Description>
<rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person">
<foaf:name>Jimmy Wales</foaf:name>
</rdf:Description>
<rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person">
<foaf:mbox>mailto:jwales@bomis.com</foaf:mbox>
</rdf:Description>
</rdf:RDF>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<genid:A4486>
a "http://xmlns.com/foaf/0.1/Person" .
foaf:Person
foaf:mbox "mailto:jwales@bomis.com" ;
foaf:name "Jimmy Wales" .
代码中有三个问题
- URI和文字(第20行和第42行)
- URI和空白节点(第18、29、40行)
rdfxml abbrev
序列化(第7行)- 您需要构建此结构:
- 相反,您正在构建如下内容:
Number Subject Predicate Object 1 genid:A4486 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person 2 genid:A4486 http://xmlns.com/foaf/0.1/name "Jimmy Wales"@en 3 genid:A4486 http://xmlns.com/foaf/0.1/mbox mailto:jwales@bomis.com 4 genid:A4486 http://xmlns.com/foaf/0.1/nick "Jimbo"@en 5 genid:A4486 http://xmlns.com/foaf/0.1/depiction http://upload.wikimedia.org/wikipedia/commons/1/19/Jimbo_Wales_in_France_cropped.jpg
<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="genid:A4486"> <rdf:type>http://xmlns.com/foaf/0.1/Person</rdf:type> </rdf:Description> <rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person"> <foaf:name>Jimmy Wales</foaf:name> </rdf:Description> <rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person"> <foaf:mbox>mailto:jwales@bomis.com</foaf:mbox> </rdf:Description> </rdf:RDF>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . <genid:A4486> a "http://xmlns.com/foaf/0.1/Person" . foaf:Person foaf:mbox "mailto:jwales@bomis.com" ; foaf:name "Jimmy Wales" .
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
猛禽世界*world=NULL;
raptor_序列化程序*rdf_序列化程序=NULL;
无符号字符*uri_字符串;
猛禽乌里*基地乌里;
猛禽_语句*triple;
世界=猛禽新世界();
uri_string=raptor_uri_filename_to_uri_string(argv[1]);
基本uri=猛禽新uri(世界,uri字符串);
rdf_serializer=raptor_new_serializer(世界,rdfxml缩写);
raptor_序列化程序_start_to_file_句柄(rdf_序列化程序、基本uri、标准输出);
常量无符号字符*foaf_前缀=(常量无符号字符*)“foaf”;
raptor_uri*foaf_uri=raptor_new_uri(world,(const unsigned char*)”http://xmlns.com/foaf/0.1/");
raptor_序列化程序_set_名称空间(rdf_序列化程序、foaf_uri、foaf_前缀);
常量无符号字符*rdf_前缀=(常量无符号字符*)“rdf”;
raptor_uri*rdf_uri=raptor_new_uri(world,(const unsigned char*)”http://www.w3.org/1999/02/22-rdf-syntax-ns#");
raptor_序列化器_集_名称空间(rdf_序列化器、rdf_uri、rdf_前缀);
{
raptor_语句*triple=NULL;triple=raptor_新语句(world);
三重->主题=raptor_new_term_from_blank(world,(const unsigned char*)“b1”);
triple->predicate=raptor\u new\u term\u from\u uri\u string(world,(const unsigned char*)”http://xmlns.com/foaf/0.1/name");
triple->object=raptor\u new\u term\u from\u literal(world,(无符号字符*)“Jimmy Wales”,NULL,NULL);
raptor_serializer_serializer_语句(rdf_serializer,triple);raptor_free_语句(triple);
}
{
raptor_语句*triple=NULL;triple=raptor_新语句(world);
三重->主题=raptor_new_term_from_blank(world,(const unsigned char*)“b1”);
triple->predicate=raptor\u new\u term\u from\u uri\u string(world,(const unsigned char*)”http://xmlns.com/foaf/0.1/mbox");
triple->object=raptor\u new\u term\u from\u uri\u string(world,(unsigned char*)”mailto:jwales@bomis.com");
raptor_serializer_serializer_语句(rdf_serializer,triple);raptor_free_语句(triple);
}
{
raptor_语句*triple=NULL;triple=raptor_新语句(world);
三重->主题=raptor_new_term_from_blank(world,(const unsigned char*)“b1”);
triple->predicate=raptor\u new\u term\u from\u uri\u string(world,(const unsigned char*)”http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
triple->object=raptor\u new\u term\u from\u uri\u string(world,(unsigned char*)”http://xmlns.com/foaf/0.1/Person");
raptor_serializer_serializer_语句(rdf_serializer,triple);raptor_free_语句(triple);
}
raptor_serializer_serializer_end(rdf_serializer);
raptor_free_序列化程序(rdf_序列化程序);
猛禽自由uri(基本uri);
raptor_空闲_内存(uri_字符串);
猛禽自由世界(世界);
返回0;
}
输出为:
吉米·威尔士
之所以生成ns0名称空间,是因为您将名称放入了名称空间http://xmlns.com/foaf/0.1/Person/
而不是http://xmlns.com/foaf/0.1/
。谢谢你的提示!这解决了我的一部分问题。我相应地修改了代码。现在只需要解决vs标签问题!我想你会发现这两种形式是等价的RDF。您可以尝试将两者序列化为Turtle并进行比较。三元组格式的三元组更容易阅读。我不使用猛禽,所以我不能提供任何其他东西。我添加了海龟输出。顺便问一下:你画了上面的数字吗?