Skip to content
Advertisement

Keep prefixes in loading and writing TTL file with RDF4J

I need to load a Turtle TTL file, add some triples, then save it again. The original TTL file has a big header containing the prefixes for some namespaces.

JavaScript

After I load the file and add the triples, the saved TTL won’t contain the prefixes, unless I specify them manually, one by one. Is there a way to keep the namespaces already present in the loaded file?

This is my Java code:

JavaScript

Advertisement

Answer

The problem is that your code for writing to file explicitly only writes the statements themselves, not the namespaces stored in your model object.

You could fix it by adding something like this:

JavaScript

However, more generally, your code to write to file is more complex than it needs to be. A far simpler approach to writing your model object to file would be something like this:

JavaScript

or if you prefer to use a RDFWriter object (so you can tweak the config):

JavaScript

Both of these are shorter, easier code, and they take care of namespace handling for you as well.

Similarly, reading the file can also be done more conveniently:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement