简体   繁体   English

读取RDF不起作用

[英]Reading RDF does not work

I am trying to road a foaf file: 我正在尝试传送foaf文件:

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class Testbed {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();

        try {
                model.read("http://www.csail.mit.edu/~lkagal/foaf", "RDF/XML"); 
        }
        catch(Exception ex) {
            System.out.println(ex.toString());
        }
    }
}

I am getting the following exception: 我收到以下异常:

org.apache.jena.riot.RiotException: [line: 1, col: 50] White spaces are required between publicId and systemId. org.apache.jena.riot.RiotException:[line:1,col:50] publicId和systemId之间需要空格。

I do not understand what this exception means. 我不明白这个例外的含义。 How can I fix it. 我该如何解决。 Am I using the wrong format (does not look like "TURTLE" or any other format)? 我使用了错误的格式(看起来不像“ TURTLE”或任何其他格式)吗?

My environment (Windows 10 x64, apache-jena-3.1.1): 我的环境(Windows 10 x64,apache-jena-3.1.1):

java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b15) Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode Java版本“ 1.8.0_112” Java™SE运行时环境(内部版本1.8.0_112-b15)Java HotSpot(TM)64位服务器VM(内部版本25.112-b15,混合模式

The URL http://www.csail.mit.edu/~lkagal/foaf is actually redirecting to http://people.csail.mit.edu/lkagal/foaf . URL http://www.csail.mit.edu/~lkagal/foaf实际上是重定向到http://people.csail.mit.edu/lkagal/foaf The presence of a redirect is the cause of the error. 重定向的存在是导致错误的原因。

The problem was already reported and fixed in the development branch of Jena (bug [JENA-1263] ). 该问题已经在Jena的开发分支中报告并解决(错误[JENA-1263] )。

Analysis 分析

Apache Jena uses Apache HttpClient for connection handling. Apache Jena使用Apache HttpClient进行连接处理。 In particular, Jena 3.1.0 uses HttpClient 4.2.6 which was updated to HttpClient 4.5.2 in Jena 3.1.1 . 特别是, Jena 3.1.0使用HttpClient 4.2.6 ,该版本已在Jena 3.1.1更新为HttpClient 4.5.2

As @potame pointed out, the issue is not present using Jena 3.1.0 , the reason is that it creates a connection which by default supports various features, including automatically following redirects (it uses new SystemDefaultHttpClient() ). 正如@potame指出的那样,使用Jena 3.1.0不会出现此问题,原因是它创建了一个默认支持各种功能的连接,包括自动跟随重定向(它使用new SystemDefaultHttpClient() )。

On the contrary, with the update of HttpClient , in Jena 3.1.1 the code was modified to create a more minimal type of connection that is unable to follow redirects (it uses HttpClients.createMinimal() ). 相反,随着HttpClient的更新,在Jena 3.1.1对代码进行了修改,以创建一种无法跟踪重定向的更简单的连接类型(它使用HttpClients.createMinimal() )。

What happens is that, instead of reaching your foaf file, it just retrieves the redirect message which is: 发生的是,它没有到达您的foaf文件,而是检索了以下重定向消息:

name="[xml]",ch=DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://people.csail.mit.edu/lkagal/foaf">here</a>.</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at www.csail.mit.edu Port 80</address>
</body></html>

and then tries to parse it with Apache Xerces which is actually the one that throws the exception (you can see that by using ex.printStackTrace() instead of System.out.println(ex.toString()) ): 然后尝试使用Apache Xerces解析它,它实际上是引发异常的代码(您可以使用ex.printStackTrace()而不是System.out.println(ex.toString())看到此异常):

...
at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:282)
at org.apache.xerces.impl.XMLScanner.reportFatalError(XMLScanner.java:1467)
at org.apache.xerces.impl.XMLScanner.scanExternalID(XMLScanner.java:1001)
...

Solutions 解决方案

  • use the direct URL, http://people.csail.mit.edu/lkagal/foaf 使用直接网址http://people.csail.mit.edu/lkagal/foaf
  • use a previous version of Jena 使用早期版本的Jena
  • use the development branch of Jena 使用Jena的发展分支
  • provide Jena with your own "redirect capable" connection, to be used instead of the default one; Jena提供您自己的“具有重定向功能”的连接,以代替默认的连接; you can do so calling the method HttpOp.setDefaultHttpClient prior to use model.read , for example: 你可以这样做调用方法HttpOp.setDefaultHttpClient在使用前model.read ,例如:

     HttpOp.setDefaultHttpClient(HttpClientBuilder.create().build()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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