繁体   English   中英

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

[英]how to parse RDF/XML using rdflib.js or rdf-parser-rdfxml in Node.js

我正在尝试读取 rdf/xml 文件并可能以 JSON 或 JavaScript 对象或任何其他格式解析它。 我尝试搜索很多节点库,但找不到任何好的例子。 所有这些都提供了一些没有适当文档的 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 返回 Object 而不是 String。 所以修改后的来源在这里:

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);
}

暂无
暂无

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

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