Convert rdf to xml with python

Viewed 690

Hi I have this RDF Input and I want make it to xml style with python , now how can I do?

@prefix Person: <http://example.org/> .

Person:Sophie Person:gender "women" ;
    Person:likes Person:Peter .

Person:Frank Person:bornIn "1949" ;
    Person:gender "Men" ;
    Person:married Person:Susie .

Person:Peter Person:father Person:Frank ;
    Person:gender "Men" ;
    Person:likes Person:Sandra ;
    Person:mother Person:Susie .

Person:Sandra Person:bornIn "1980" ;
    Person:gender "women" .

Person:Susie Person:bornIn "1956" ;
    Person:gender "women" .



1 Answers

You can install the rdflib package and use it to convert between the RDF formats. In your case it looks like the data is stored in the "notation3" syntax. To parse this you need to specify the format when reading the graph as 'n3'.

I am using StringIO in place of actual files below, but the concept is the same.

import rdflib
from io import StringIO, BytesIO

str_in = StringIO('''@prefix Person: <http://example.org/> .

Person:Sophie Person:gender "women" ;
    Person:likes Person:Peter .

Person:Frank Person:bornIn "1949" ;
    Person:gender "Men" ;
    Person:married Person:Susie .

Person:Peter Person:father Person:Frank ;
    Person:gender "Men" ;
    Person:likes Person:Sandra ;
    Person:mother Person:Susie .

Person:Sandra Person:bornIn "1980" ;
    Person:gender "women" .

Person:Susie Person:bornIn "1956" ;
    Person:gender "women" .'''
)

g = rdflib.Graph()
g.parse(str_in, format='n3')

At this point the data is loaded into the graph. If you want write it out to a new format, such as XML, you can do so via the following. To write to a file, just put in a file path as the first argument of g.serialize().

g.serialize('path/to/file.xml', format='pretty-xml')

with open('path/to/file.xml') as fp:
    print(fp.read())
# prints:
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:Person="http://example.org/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://example.org/Sophie">
    <Person:likes>
      <rdf:Description rdf:about="http://example.org/Peter">
        <Person:gender>Men</Person:gender>
        <Person:likes rdf:resource="http://example.org/Sandra"/>
        <Person:father rdf:resource="http://example.org/Frank"/>
        <Person:mother rdf:resource="http://example.org/Susie"/>
      </rdf:Description>
    </Person:likes>
    <Person:gender>women</Person:gender>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.org/Susie">
    <Person:gender>women</Person:gender>
    <Person:bornIn>1956</Person:bornIn>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.org/Frank">
    <Person:married rdf:resource="http://example.org/Susie"/>
    <Person:bornIn>1949</Person:bornIn>
    <Person:gender>Men</Person:gender>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.org/Sandra">
    <Person:bornIn>1980</Person:bornIn>
    <Person:gender>women</Person:gender>
  </rdf:Description>
</rdf:RDF>
Related