Write OWL Ontology Model to File using Apache Jena

Write OWL Ontology Model to File using Apache Jena.

Javadoc available at https://www.javatapas.com/docs/javatapas/jena/WriteOwlOntologyModelToFileUsingJena.html


private static void writeOwlOntologyModelToFileUsingJena(OntModel ontModel, OutputStream outputStream) throws IOException {

	org.apache.jena.rdf.model.RDFWriterI w = ontModel.getWriter("RDF/XML");

	w.setProperty("attributeQuoteChar","\"");
	w.setProperty("showXMLDeclaration","true");
	w.setProperty("tab","1");

	String base = ontModel.getNsPrefixURI("").substring(0, ontModel.getNsPrefixURI("").length() - 1);
	w.setProperty("xmlbase", base);
	w.write(ontModel, outputStream, base);

	outputStream.flush();

}

public static void writeModel(OntModel ontModel, String directory, String fileName) throws IOException {

	String fullFileName = directory + File.separator + fileName;

	Files.createDirectories(Paths.get(directory));

	File file = new File(directory + File.separator + fileName);

	OutputStream outputStream = new FileOutputStream(file);

	writeOwlOntologyModelToFileUsingJena(ontModel, outputStream);

	outputStream.close();

}