gpt4 book ai didi

javascript - 如何在 Node.js 中使用 rdflib.js 或 rdf-parser-rdfxml 解析 RDF/XML

转载 作者:太空宇宙 更新时间:2023-11-04 00:42:36 27 4
gpt4 key购买 nike

我正在尝试读取 rdf/xml 文件并可能以 JSON 或 JavaScript 对象或任何其他格式解析它。我尝试搜索很多 Node 库,但找不到任何好的示例。所有这些都提供了一些没有适当文档的API(或者他们提供了但我没有正确理解)。这是我尝试过的方法。

var fs = require('fs'),
rdfParser = require('rdf-parser-rdfxml');

var res=rdfParser(__dirname+'/1.xml');
console.log(res);

这里是 1.xml 文件

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:oneM2M="http://www.onem2m.org/ontology/Base_Ontology#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:cfso="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
<oneM2M:Thing rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#KETICF">
<cfso:hasSpecies>
<cfso:Species rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Type1"/>
</cfso:hasSpecies>
<cfso:hasSPName>
<cfso:SPName rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Maxfor"/>
</cfso:hasSPName>
<cfso:hasLocation>
<cfso:Location rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Buyeo"/>
</cfso:hasLocation>
<cfso:hasCropName>
<cfso:CropName rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#CharryTomato">
<cfso:hasSpecies rdf:resource="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Type1"/>
</cfso:CropName>
</cfso:hasCropName>
<cfso:hasControlMode>
<cfso:ControlMode rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#A"/>
</cfso:hasControlMode>
</oneM2M:Thing>
</rdf:RDF>

我也以非常类似的方式尝试过 rdflib.js。

var fs = require('fs'),
$rdf = require('rdflib');

//var parser = new xml2js.Parser();
fs.readFile(__dirname + '/1.xml', function(err, data) {
// Fetch data via a regular AJAX call, load from a file, or pass in a literal
// string. In this example, it was loaded from 'https://fred.me/profile'
var store = $rdf.graph() // Init a new empty graph
var contentType = 'application/rdf+xml'
var baseUrl = ""
var parsedGraph = $rdf.parse(data, store, baseUrl, contentType);

$rdf.parse(data,function(triples){
for (var i in triples){
console.log(triples[i]);
}
})
});

帮帮我吧:)提前谢谢

最佳答案

我发现 fs.readFile 返回对象而不是字符串。修改后的源码在这里:

var fs = require('fs'),
$rdf = require('rdflib');

var rdfData=fs.readFileSync(__dirname+'/1.xml').toString();

var store=$rdf.graph();
var contentType='application/rdf+xml';
var baseUrl="http://IoFTriples.com";
try{
$rdf.parse(rdfData,store,baseUrl,contentType);

var stms = store.statementsMatching(undefined, undefined , undefined);
for (var i=0; i<stms.length;i++) {
var stm = stms[i]
console.log(stm) // the WebID of a friend

}
} catch(err){
console.log(err);
}

关于javascript - 如何在 Node.js 中使用 rdflib.js 或 rdf-parser-rdfxml 解析 RDF/XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36053643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com