【问题标题】:File/ontology (turtle, N3, JSON, RDF-XML) etc to .ttl file conversions in java文件/本体(turtle、N3、JSON、RDF-XML)等到 java 中的 .ttl 文件转换
【发布时间】:2020-02-22 17:19:02
【问题描述】:

我想问一下Java中是否有一种方法可以读取,基本上是任何文件格式(N3、JSON、RDF-XML)等,然后将其转换为turtle(.ttl)。我在谷歌上搜索了一些想法,但它们主要只是解释特定的文件类型以及如何将文件类型转换为 RDF,而我想要它以另一种方式。

编辑(按照答案中给出的代码示例):

if(FilePath.getText().equals("")){
FilePath.setText("Cannot be empty");

}else{

try {
    // get the inputFile from file chooser and setting a text field with
    // the path (FilePath is the variable name fo the textField in which the 
    // path to the selected file from file chooser is done earlier)
    FileInputStream fis = new FileInputStream(FilePath.getText());
    // guess the format of the input file (default set to RDF/XML)
    // when clicking on the error I get take to this line.
    RDFFormat inputFormat = Rio.getParserFormatForFileName(fis.toString()).orElse(RDFFormat.RDFXML);
    //create a parser for the input file and a writer for Turtle
    RDFParser rdfParser = Rio.createParser(inputFormat);
    RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, 
            new FileOutputStream("./" + fileName + ".ttl"));

    //link parser to the writer
    rdfParser.setRDFHandler(rdfWriter);
    //start the conversion
    InputStream inputStream = fis;
    rdfParser.parse(inputStream, fis.toString());

    //exception handling
} catch (FileNotFoundException ex) {
    Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
 }
}

我已将“eclipse-rdf4j-3.0.3-onejar.jar”添加到 NetBeans 的 Libraries 文件夹中,现在当我运行该程序时,我不断收到此错误:

线程“AWT-EventQueue-0”中的异常 java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 在 org.eclipse.rdf4j.common.lang.service.ServiceRegistry.(ServiceRegistry.java:31)

任何帮助或建议将不胜感激。谢谢。

【问题讨论】:

    标签: java file rdf converters turtle-rdf


    【解决方案1】:

    是的,这是可能的。一种选择是为此目的使用Eclipse RDF4J,或者更具体地说,使用它的Rio parser/writer toolkit

    这是一个使用 RDF4J Rio 的代码示例。它根据文件扩展名检测输入文件的语法格式,并直接将数据写入新文件,采用 Turtle 语法:

    // the input file
    java.net.URL url = new URL(“http://example.org/example.rdf”);
    
    // guess the format of the input file (default to RDF/XML)
    RDFFormat inputFormat = Rio.getParserFormatForFileName(url.toString()).orElse(RDFFormat.RDFXML);
    
    // create a parser for the input file and a writer for Turtle format
    RDFParser rdfParser = Rio.createParser(inputFormat);
    RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE,
                   new FileOutputStream("/path/to/example-output.ttl"));
    
    // link the parser to the writer
    rdfParser.setRDFHandler(rdfWriter);
    
    // start the conversion
    try(InputStream inputStream = url.openStream()) {
       rdfParser.parse(inputStream, url.toString());
    }
    catch (IOException | RDFParseException | RDFHandlerException e) { ... }
    

    有关更多示例,请参阅RDF4J documentation

    edit关于您的NoClassDefFoundError:您的类路径中缺少必要的第三方库(在这种特殊情况下,是日志库)。

    与其使用 onejar,不如使用 Maven(或 Gradle)来设置您的项目。请参阅development environment setup 注释,或者有关更详细的分步指南,请参阅this tutorial(本教程使用 Eclipse 而不是 Netbeans,但有关如何设置 maven 项目的要点在 Netbeans 中将非常相似)。

    如果你真的不想使用 Maven,你也可以只是download the RDF4J SDK,这是一个 ZIP 文件。解压它,只需将lib/ 目录中的所有jar 文件添加到Netbeans。

    【讨论】:

    • @JeenBroekstra 好的,我明白了。但也许下载页面上的提示可能会避免混淆。人们还需要知道哪些 3rd 方 JAR 必须添加到类路径中。并不是说我现在建议不要使用 Maven 或 Gradle ...
    • @AKSW 它在文档中被提及,但你说得对,从下载页面上并没有立即清楚。另外:见github.com/eclipse/rdf4j/issues/1791
    【解决方案2】:

    另一种选择是使用Apache Jena

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-10
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      相关资源
      最近更新 更多