如何以原始格式编写RDF

时间:2017-03-10 16:05:51

标签: rdf4j

RDF4J新手问题 -

从文件加载此RDF时:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<foaf:Person>
 <foaf:name>My Name</foaf:name>
</foaf:Person>

</rdf:RDF>

使用此代码

String fileName = "foaf.rdf";
try {
    InputStream is = new FileInputStream(fileName);
    Model model = Rio.parse(is, "", RDFFormat.RDFXML);
    System.out.println();
    Rio.write(model, System.out, RDFFormat.RDFXML);
    System.out.println();
    is.close();
}
catch (Exception ex) {
    System.out.println(ex.toString());
}

输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:nodeID="node1basf0r6vx1">
    <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
    <foaf:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">My Name</foaf:name>
</rdf:Description>

</rdf:RDF>

我如何以原始格式打印出RDF?

1 个答案:

答案 0 :(得分:2)

RDF4J还具有漂亮的打印功能。也许这可以帮助你输出。

您只需创建一个编写器,然后将pretty print设置为true。

Rio.createWriter(format, outputStream).set(BasicWriterSettings.PRETTY_PRINT, true)