Jena:RDF / XML输出中只能包含格式正确的绝对URIref:

问题描述 投票:0回答:1

我正在从构建RDF的csv文件进行解析器。现在,它只是将csv的标头及其值添加为属性。当我尝试将输出编写为XML时,出现此错误:

Caused by: org.apache.jena.shared.BadURIException: Only well-formed absolute URIrefs can be included in RDF/XML output: <A> Code: 57/REQUIRED_COMPONENT_MISSING in SCHEME: A component that is required by the scheme is missing.

但是当我将其写为json时,我得到正确的输出。

有人知道我做错了吗?代码:

public List<Model> createRDF(File file) throws Exception { //TODO implement custom Exceptions
    Csv csv = CsvReader.convertFileToCsv(file);
    List<Model> modelList = new ArrayList<>();
    for(int i = 1; i < csv.lines.length; i++) {
        Model model = ModelFactory.createDefaultModel();
        Resource r = model.createResource( "http://provisionalUri.com/" + i);
        addProperties(r, csv, model, i);
        modelList.add(model);
    }


    return modelList;
}

private void addProperties(Resource r, Csv csv, Model model, int i) {
    for(int j = 0; j < csv.lines[i].length; j++) { // if the columns have different length this will cause problems
        Property property = model.createProperty(csv.headers[j]);
        Literal value = model.createLiteral(csv.lines[i][j]);
        model.add(r, property, value);
    }

}

写作:

List<Model> models = service.createRDF(new File("./src/test/java/resources/test/Bienes_declarados_Patrimonio_mundial_de_la_UNESCO_en_España.csv"));
        for(Model model: models){
            RDFDataMgr.write(System.out, model, Lang.RDFXML);
        }
java apache uri jena
1个回答
0
投票

RDF / XML对URI的要求比JSON-LD更高,因为属性将被写为XML qnames。

model.createProperty(csv.headers[j]);不是合法的绝对URI,除非您注意CSV标头。

需要两件事:

  1. 如果它的字符对URI不利,则需要对“ csv.headers [j]”进行编码。
  2. 需要适当的URI,例如在前面添加“ http://HOST/”:

model.createProperty("http://example/"+encode(csv.headers[j]));

© www.soinside.com 2019 - 2024. All rights reserved.