简体   繁体   中英

How to load an N-Triples model and obtain the namespaces from RDF file?(newbie)

I have an RDF/XML file like this:

<rdf:RDF
xmlns:go="http://www.geneontology.org/dtds/go.dtd#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
        <go:term rdf:about="http://www.geneontology.org/go#GO:0000001">
            <go:accession>GO:0000001</go:accession>
            <go:name>mitochondrion inheritance</go:name>
            <go:synonym>mitochondrial inheritance</go:synonym>
            <go:definition>The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton.</go:definition>
            <go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048308" />
            <go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048311" />
        </go:term>
</rdf:RDF>

Then I have to convert this RDF/XML into the N-Triples format, and I have done this automatically with Jena:

FileManager.get().addLocatorClassLoader(Converter.class.getClassLoader());
    Model model = FileManager.get().loadModel("smallfacts.rdf");
    try {

          File file= new File("outputsmall.txt");
          model.write(new FileOutputStream(file), "N-Triples");
        } catch (IOException e) {
          e.printStackTrace();

But then I need to shorten each predicate with a prefix. I have to obtain something like this:

<http://www.geneontology.org/go#GO:2000391> is_a <http://www.geneontology.org/go#GO:2000389>.
<http://www.geneontology.org/go#GO:2000391> accession "GO:2000391".

I have read about getNSPrefix() and have made some attempts, such as:

FileManager.get().addLocatorClassLoader(Converter.class.getClassLoader());
    Model model = FileManager.get().loadModel("smallfacts.rdf");
    try {

          File file= new File("outputsmall.txt");
          model.getNsPrefixURI("http://www.geneontology.org/dtds/go.dtd#");
          model.write(new FileOutputStream(file), "N-Triples", "http://www.geneontology.org/dtds/go.dtd#");
        } catch (IOException e) {
          e.printStackTrace();

How do I use getNsPrefixURI() in this case? Should I use another method?

Try writing your RDF data with RDFDataMgr and RDFFormat.TURTLE_FLAT. https://jena.apache.org/documentation/io/rdf-output.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM