我正在从构建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);
}
RDF / XML对URI的要求比JSON-LD更高,因为属性将被写为XML qnames。
model.createProperty(csv.headers[j]);
不是合法的绝对URI,除非您注意CSV标头。
需要两件事:
model.createProperty("http://example/"+encode(csv.headers[j]));